javascript doesn´t work in joomla 3.0 - javascript

I have a javascript code that shows a div content depending on the select-box option value.
The code works fine in html, but in the Joomla 3.0 he doesn't.
inside the php of Joomla i have this script:
<?php
//jQuery Framework in noConflict mode
JHtml::_('jquery.framework', false);
$document = JFactory::getDocument();
$document->addScriptDeclaration('
jQuery(document).ready(function(){
jQuery("select").change(function(){
jQuery( "select option:selected").each(function(){
if($(this).attr("value")=="3"){
$(".box").hide();
$(".choose").show();
}
if($(this).attr("value")=="5"){
$(".box").hide();
$(".green").show();
}
if($(this).attr("value")=="6"){
$(".box").hide();
$(".blue").show();
}
if($(this).attr("value")=="7"){
$(".box").hide();
$(".red").show();
}
});
}).change();
});
');
// Disallow direct access to this file
defined ( '_JEXEC' ) or die ( 'Restricted access' );
?>
And this is the drop-down form:
<select id="profiletypes" name="profiletypes" class="select required pt-font-color">
<option value="3">option3</option>
<option value="5">option5</option>
<option value="6">option6</option>
<option value="7">option7</option>
</select>
<div class="choose box">You have to select <strong>any one option</strong> so i am here</div>
<div class="red box">You have selected <strong>red option</strong> so i am here</div>
<div class="green box">You have selected <strong>green option</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue option</strong> so i am here</div>
Along with the CSS:
//select::-ms-expand { /* for IE 11 */
display: none; }
div.registerProfileType .pt-font-color{ background:
url(/images/sports-5.png) 96% / 15% no-repeat #6d9dc0;
color:#fff; }
.box{
padding: 20px;
display: none;
margin-top: 20px;
border: 1px solid #000;
}
.red{ background: #ff0000;
}
.green{ background: #00ff00;
}
.blue{ background: #0000ff;
}
.choose{background: #ffffff;
}
</style>
I know that in joomla 3.0 we need to use Jquery instead of $, if we call JQuery normally, i have tryed that, but it doesn´t work.
What am I can possibly doing wrong? I shall be grateful for any help.

You are still using $ within the .each loop which is causing the issue. Another issue is the loop causes multiple redirects. If you use the below block of code in your PHP script, it should work as intended (option 3 selected by default but picking another option changes the colour of the div etc).
// jQuery Framework in noConflict mode
JHtml::_('jquery.framework', false);
$document = JFactory::getDocument();
$js =<<<JS
jQuery(document).ready(function(){
jQuery("#profiletypes").change(function(){
if(jQuery(this).attr("value")=="3"){
jQuery(".box").hide();
jQuery(".choose").show();
}
if(jQuery(this).attr("value")=="5"){
jQuery(".box").hide();
jQuery(".green").show();
}
if(jQuery(this).attr("value")=="6"){
jQuery(".box").hide();
jQuery(".blue").show();
}
if(jQuery(this).attr("value")=="7"){
jQuery(".box").hide();
jQuery(".red").show();
}
}).change();
});
JS;
// The above 'JS' must be at the start of the line, not tabbed in
// Add the JS
$document->addScriptDeclaration($js);

Related

Change only one select style in jquery plugin

So I am using chosen - jquery plugin for selects.
Say I have four selects on the webpage, and I want the last one to open upward. I know how to change style to open select upward, but I can't figure out how to do this for only one select. It is probably trivial but I couldn't find the answer anywhere
To install it I downloaded files chosen.jquery.min.js as well as chosen.css and I put following code into my js file that handles all selects:
$(document).ready(function() {
$('select').chosen({ width: '100%', disable_search_threshold: 9 });
});
I should probably add classes to selects, add style that I want to override to chosen2.css, right? But then how to connect those particular selects with new css file?
Here is a fiddle of what I am trying to accomplish:
fiddle
EDITED:
I modified your fiddle, just put your last select inside a div and apply your css just to selects inside that div:
Drop Up
<div class="drop-up">
<div class="box" style="margin-left: 50px; margin-top: 15px;">
<select class="chosen-select">
<option selected="selected">10</option>
<option>20</option>
<option>50</option>
<option>100</option>
</select>
</div>
</div>
CSS
.drop-up .chosen-container .chosen-drop {
border-bottom: 0;
border-top: 1px solid #aaa;
top: auto;
bottom: 40px;
}
Let me know if this is what you are looking for :)
I think you can use classes or Ids on your select tag
for example :
$(document).ready(function() {
$('select.last').chosen({ width: '100%', disable_search_threshold: 9 });
});
or
$(document).ready(function() {
$('#last').chosen({ width: '100%', disable_search_threshold: 9 });
});

Hide 'Dropdown' select

Good afternoon, do you know if there is any way to hide the 'Dropdown' that appears with the options of a select when clicking on it? When click add a class to an element to show a hidden div but for something less than 1 sec you see the dropdown.
If you can help me, I would be grateful
A greeting.
$('.select').on('click', function(e) {
e.preventDefault();
$(this).attr("disabled", true);
$('.options_select').addClass('show');
});
No, is a replaced element, that is rendered by the operating system. There is no CSS option to hide the drop-down.
There's some notes on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#Styling_with_CSS
I think you can get what you're looking for like this: https://codepen.io/gerard-payne/pen/NVjppd
select {
appearance: textfield;
-webkit-appearance: textfield;
}
option {
display: none;
}
but also setting disabled and adding a font color.
<select disabled="disabled">
<option>2000</option>
<option>2001</option>
<option>2002</option>
</select>
select[disabled="disabled"] {
color: black;
}
don't add the options to the list or add something like:
CSS:
.select > option {
display: none;
}
$(".select").on('click',function(){
$('.submenu').toggle();
})
.select{ border:solid 1px #000; display:inline-block; padding:20px; cursor:pointer;}
.submenu{ background:#345; display:none;}
.submenu a{color:#fff;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="select">Select</div>
<div class="submenu">
option
option
option
option
</div>
i think you want this? if not let me know

Right-align dropdown menu in select2

Is there a way to right-align the dropdown menu with dropdownAutoWidth: true in a select2, instead of the standard left-align? I want the select to stay the same, but the dropdown to move to the left so it aligns with the select on the right side. See snippet below with the wrong alignment...
Note: I want different sizes of the select and the dropdown just as shown.
Edit // Updated with suggestion to add direction: rtl; text-align: right;. This only right-aligns the text. I want the whole dropdown to right-align with the selected option above it. E.g. the dropdown should stick out to the left of the selected option, not to the right.
Edit 2 // Updated with .select2-dropdown { left: -69px !important; } This makes it look the way I want, but is there a way to not have to set a fixed value of -69px?
$(".form-control").select2({
width: '100px',
dropdownAutoWidth : true,
});
.select2-container--default .select2-results > .select2-results__options { direction: rtl; text-align: right; }
.select2-dropdown {
left:-69px !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
I want it to look like this, but not with a fixed value to achieve it.
As you want to have different sizes of the select and the dropdown, here is my solution using select2 built-in options and some CSS, but maybe not perfect one.
Making rtl is dead easy, just use dir, but we need to pass some other options as shown below:
$('select').select2({
dir: "rtl",
dropdownAutoWidth: true,
dropdownParent: $('#parent')
});
The key here is dropdownParent, also you need to edit your HTML as follow:
<div id='parent'>
<select>
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>
</div>
And finally you need some CSS:
#parent {
/* can be any value */
width: 300px;
text-align: right;
direction: rtl;
position: relative;
}
#parent .select2-container--open + .select2-container--open {
left: auto;
right: 0;
width: 100%;
}
You can see it in action here on codepen
If you have more that one select, you need some JS code to handle dropdownParent option. btw your life will be easier if you remove dropdownAutoWidth option ;)
I managed to make it work slightly different. I needed to expand the dropdown to the left while keeping the options texts aligned left. I thought it's worth sharing. Here is the setup:
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2#4.1.0-rc.0/dist/js/select2.min.js"></script>
<div id="select2-container">
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option>purple</option>
</select>
</div>
CSS
.select2-container.select2-container--default.select2-container--open {
right: auto;
left: unset !important;
}
.select2-container.select2-container--default.select2-container--open .select2-dropdown.select2-dropdown--below {
width: fit-content !important;
left: unset;
right: 0;
}
JavaScript
$('.form-control').select2({ dropdownParent: $('#select2-container') });
Result looks like:
If still looking for an alternative method. I recently needed to achieve this as well and the RTL answer was not a viable solution for my project. If you are using the select2.js(4.0.6) and do not mind modifying the script you can replace the following code and achieve right positioning with adding a class to the target element.
Look for lines 4381 to 4384
var css = {
left: offset.left,
top: container.bottom
};
Replace with the following
var containerWidth = this.$container.outerWidth()
var right = ($("body").outerWidth() - (offset.left + containerWidth));
if (this.$element.hasClass("right-align")) {
var css = {
right: right,
top: container.bottom
};
} else {
var css = {
left: offset.left,
top: container.bottom
};
}
Add the class right-align to your target select input.
<select id="client_select" class="form-control right-align">
<option value="01">Example One</option>
<option value="02">Example Two</option>
<option value="03">Example Three</option>
</select>
That should be all you need. If the target element has the class then the code will position the dropdown using right instead of left positioning. Should work the same if container is set to a parent. However, this is not thoroughly tested. It may not be an elegant solution, but it does not require overriding anything with CSS. Should do the trick until an update is made to add an option for handling right aligned dropdowns. Hope it helps.
init select with dir: "rtl" such as:
$('select').select2({
dir: "rtl,
...
...
...
...
})
option tag of select cannot be modified by css it need to be done jquery plugin
http://selectric.js.org/demo.html
It should help you my friend
I think it will help you to get what exactly you need.if u wont get it ping me back
select {
text-align-last: right;
}
option {
direction: rtl;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<select class="form-control">
<option selected="selected">orange</option>
<option>white</option>
<option selected="selected">purple</option>
</select>

Style Select Element with same style as the selected option

I am trying to style a select element that is created in php. The select code is very simple and I can style the text color of the drop down. But this doesn't style the select element when a option is selected. Also this code doesn't work on Safari at all.
Code:
<div class="theme">
<select class="theme" name="select_theme">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
CSS:
.theme {
width: 200px;
}
.theme select {
width: 200px;
background-color: rgba(200,200,200,0.8);
}
.theme select option[value="red"] {
color: red;
}
.theme select option[value="green"] {
color: green;
}
.theme select option[value="blue"] {
color: blue;
}
How do I style the default select so that it matches the option style rule?
How to get this to work on Safari? Possibly IE I haven't checked it yet no Windows system ATM.
Is there a way to get these rules to display an image as well? I've tried a few things but seems it requires Javascript which I try to avoid when I can.
I made a JSFiddle to demonstrate this.
There's no way you can accomplish this without JavaScript:
$('select.theme').change(function () {
$(this).css('color', $(this).find('option:selected').css('color'));
}).change();
Here's your fiddle: http://jsfiddle.net/uCmSR/2/

How to handle alternating rows dynamically?

Consider the following example: (live demo here)
HTML:
<div class="wrapper">
<div city_id="1" class="odd">Street Name #1 in city 1</div>
<div city_id="3" class="even">Street Name #2 in city 3</div>
<div city_id="2" class="odd">Street Name #3 in city 2</div>
<div city_id="1" class="even">Street Name #4 in city 1</div>
<div city_id="1" class="odd">Street Name #5 in city 1</div>
<div city_id="3" class="even">Street Name #6 in city 3</div>
<div city_id="2" class="odd">Street Name #7 in city 2</div>
<div city_id="3" class="even">Street Name #8 in city 3</div>
<div city_id="1" class="odd">Street Name #9 in city 1</div>
</div>
<select>
<option value="">Please select...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
CSS:
.odd {
background-color: #777;
}
.even {
background-color: #aaa;
}
JS:
$(function() {
$("select").change(function() {
var city_id = $("option:selected", this).val();
$("div[city_id]").each(function() {
$(this).toggle($(this).attr("city_id") == city_id);
});
});
});
I would like to keep the alternating coloring even when some rows are hidden.
Is that possible to achieve this with pure CSS ?
If no, how would you do this using Javascript/jQuery ?
Here's a simple solution . Dynamically change the row's class while the selected index changes
http://jsfiddle.net/4Bjbc/5/
$(function() {
$("select").change(function() {
var city_id = $("option:selected", this).val();
$("div[city_id]").each(function() {
$(this).toggle($(this).attr("city_id") == city_id);
}).filter(":visible") ///filter the visible ones
.attr("class",function(index,$class){
///if you don't want to miss out the other classes
return index%2 == 0 ? $class.replace("odd","even") : $class.replace("even","odd");
});
});
});
you need from javascript to set the class odd or even by walking through the items and if they are visible alternate the class
You can use CSS for it:
.wrapper div:nth-child(even) { background-color: #777; }
.wrapper div:nth-child(odd) { background-color: #aaa; }
However, it won't take hidden rows into account. To achieve this, you need to restrict the div selector even more:
.wrapper div.visible:nth-child(even) { background-color: #777; }
.wrapper div.visible:nth-child(odd) { background-color: #aaa; }
Then you just need to ensure that all visible elements have the visible class.
With jquery you could use in the change event:
$('.wrapper div:visible:even').css({
'background-color': '#777'
});
$('.wrapper div:visible:odd').css({
'background-color': '#aaa'
});
FULL CODE
$("select").change(function() {
var city_id = $("option:selected", this).val();
$("div[city_id]").each(function() {
$(this).toggle($(this).attr("city_id") == city_id);
});
$('.wrapper div:visible:even').css({
'background-color': '#777'
});
$('.wrapper div:visible:odd').css({
'background-color': '#aaa'
});
});
This way it sets the background color taking into account only visible rows
fiddle here: http://jsfiddle.net/4Bjbc/3/

Categories

Resources