$('select').change(function but on only one specific dropdown? (HTML, Javascript) - javascript

I'm trying to do that this code:
$(document).ready(function () {
$('select').change(function () {
alert(this.value);
});
Alerts a message but only when one of the various dropdowns onscreen is selected. But it works whenever I pick ANY dropdown. How can I do it so that it only works with one specific dropdown?

Simply give your respective select tag an id, then use that id for your selector in your jQuery:
$('#select1').change(function ()
{
alert(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select1">
<option value="">--Please Choose--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="select2">
<option value="">--Please Choose--</option>
<option value="A">Apple</option>
<option value="B">Banana</option>
<option value="C">Cherry</option>
</select>

Related

Show selected option with JS

I don't know how to make next:
I want that when someone selects items from combobox, that item be shown in div.
What do I need to do?
<body>
<h1>VEZBA STRUKTURA</h1>
<div id="prvi"></div>
<select name="automobili" id="automobili">
<option value="1">BMW</option>
<option value="2">AUDI</option>
<option value="3">OPEL</option>
<option value="4">FERARI</option>
<option value="5">SKODA</option>
<option value="6">HJUNDAI</option>
</select>
<script>
</script>
</body>
You need to attach an event listener that executes when the value of the select changes:
automobili.addEventListener('change', function() {
prvi.innerHTML = automobili.selectedOptions[0].textContent
})
<h1>VEZBA STRUKTURA</h1>
<div id="prvi"></div>
<select name="automobili" id="automobili">
<option value="1">BMW</option>
<option value="2">AUDI</option>
<option value="3">OPEL</option>
<option value="4">FERARI</option>
<option value="5">SKODA</option>
<option value="6">HJUNDAI</option>
</select>

Mutually exclusive select boxes

I have 2 select boxes. On change of one select box, I want the other select box to be defaulted to Select a value, ie val()==0, and vice versa. Only one select box should have a value selected at a time. How do I do this? I tried the code below but it does not work.
$("#select_one").change(function() {
if ('#select_two'.val() != 0) {
$('#select_two').val(0);
}
});
The simplest way to do this would be to place a common class on both the select. You can then select that class in jQuery and use not() to exclude the current element before resetting the value, something like this:
$('.select').change(function() {
$('.select').not(this).val(0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select-one" class="select">
<option value="0">Please select</option>
<option>Foo</option>
<option>Bar</option>
<option>Fizz</option>
<option>Buzz</option>
</select>
<select id="select-two" class="select">
<option value="0">Please select</option>
<option>Foo</option>
<option>Bar</option>
<option>Fizz</option>
<option>Buzz</option>
</select>
You may set the selectedIndex to 0:
$('select[id^="select_"]').on('change', function(e) {
$('select[id^="select_"]').not(this).prop('selectedIndex', 0);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select_one">
<option value="volvo">1</option>
<option value="saab">2</option>
<option value="mercedes">3</option>
<option value="audi">4</option>
</select>
<select id="select_two">
<option value="volvo">11</option>
<option value="saab">22</option>
<option value="mercedes">33</option>
<option value="audi">44</option>
</select>
That could be done using two simple change events, like the following example.
Else if you want to use one event you could use a common class as #Rory McCrossan answer shows..
Hope this helps.
$("#select_one").change(function() {
$('#select_two').val(0)
});
$("#select_two").change(function() {
$('#select_one').val(0)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="select_one">
<option value="0">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<br>
<select id="select_two">
<option value="0">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>

Don't show part of pull downs content?

I have some dropdowns in a form that are generated by a backoffice. At the end of each choice in the dropdown something is added between (brackets). How can I not show these brackets and their variable content? Leaving only the content before the brackets.
<select>
<option value="volvo">Volvo (30.000)</option>
<option value="saab">Saab (40.000)</option>
<option value="opel">Opel (15.000)</option>
<option value="audi">Audi (45.000)</option>
</select>
<select>
<option value="dog">Dog (300)</option>
<option value="cat">Cat (50)</option>
<option value="fish">Fish (5)</option>
</select>
Try this;
var carList=[{name:"Audi (45.000)" },{name:"Saab (40.000)"},{name:"Opel (15.000)"},{name:"Audi (45.000)"}];
var $cars=$("#cars");
$cars.empty();
carList.forEach(function(index){
$cars.append(new Option(index.name.split("(")[0].trim()))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="cars">
</select>
For all select elements
$("select").each(function(){
var $wrapper=$(this);
var $options=$wrapper.find("option");
$wrapper.empty();
$options.each(function(index){
$wrapper.append(new Option($(this).text().split("(")[0].trim()))
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<select>
<option>Fiat (32.00)</option>
<option>Audi (12.00)</option>
</select>
<select>
<option>Bmw (32.00)</option>
<option>Tofas (22.00)</option>
</select>
<select>
<option>Dog (1.00)</option>
<option>Fish (0.00)</option>
</select>

Enable/disable drop down list based on another drop down list

I have 2 drop down list
<select id="servergroup" multiple="multiple">
<option value="P1">P1</option>
<option value="P2">P2</option>
<option value="P3">P3</option>
<option value="P4">P4</option>
</select>
<select id="servername" multiple="multiple">
<option value="s597ap233">s597ap233</option>
<option value="s597dp392">s597dp392</option>
<option value="s397dp095">s397dp095</option>
</select>
I want that the second drop down list should get enabled only if we choose a value in the first drop down list. It should again get disabled if we deselect the value from the first drop down list.
May I know how can this be achieved using jQuery?
You can use the disabled attribute and using JavaScript, you can set it as false or true
function check(){
if(document.getElementById('servergroup').value!='')
document.getElementById('servername').disabled=false;
else
document.getElementById('servername').disabled=true;
}
<select onchange="check()" id="servergroup" multiple="multiple">
<option value="P1">P1</option>
<option value="P2">P2</option>
<option value="P3">P3</option>
<option value="P4">P4</option>
</select>
<select disabled id="servername" multiple="multiple">
<option value="s597ap233">s597ap233</option>
<option value="s597dp392">s597dp392</option>
<option value="s397dp095">s397dp095</option>
</select>
$('#bonusVoucher').prop('disabled',false);
jQuery('#bonusVoucher').selectpicker('refresh');
Use selectpicker like this and the most IMPORTANT part is this:
Note: Place your bootsrap selectpicker script after jquery script.
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.17/js/bootstrap-select.min.js"></script>
jQuery solution.
function change() {
if ($('#servergroup option:selected').length) {
$('#servername').attr('disabled', false);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="servergroup" multiple="multiple" onchange='change()'>
<option value="P1">P1</option>
<option value="P2">P2</option>
<option value="P3">P3</option>
<option value="P4">P4</option>
</select>
<select id="servername" multiple="multiple" disabled>
<option value="s597ap233">s597ap233</option>
<option value="s597dp392">s597dp392</option>
<option value="s397dp095">s397dp095</option>
</select>
First add
<option value="-1"> -Select Server Group- </option>
and
<option value="-1"> -Select Server Name- </option>
to Your Dropdown boxes respectively.
We can achieve requested actions using JQuery as Follows:
$(document).ready(function ()
{
//making serverName Dropdown box disabled by default.
$('#servername').prop('disabled', true);
$('#servergroup').change(function ()
{
if($(this).val == "-1")
{
$('#servername').val = "-1";
$('#servername').prop('disabled', true);
}
else
{
$('#servername').prop('disabled', false);
}
});
});

How to store multiple values from multiple select field using javascript?

I am having trouble to store all the selected options as a list, my code only sotres the first selected option and ignores the rest. How can I solve this problems?
<label for="itemlist">Items</label>
<br>
<select id="itemlist" name="itemlist" multiple="multiple" required>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<script type="text/javascript">
$(function() {
$('#itemlist').change(function() {
console.log($(this).val());
}).multipleSelect({
width: '100%'
});
});
</script>
Gus,
As you are using it jQuery already $('selector').val(), this will retreive (comma separated) all values selected on the select tag.
Here is an example I've wrote for you using the .on('click', function).
FIDDLE
Hope this helps!

Categories

Resources