select multiple dropdowns value (all) based on the first dropdown selected value - javascript

How to change the selected value for all dropdowns (childselect1, ..2, ..3) value based on the first (motherselect) selected value by Javascript only?
<select name="motherselect" id="motherselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
..
<select name="childselect1" id="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
..
<select name="childselect2" id="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
..
<select name="childselect3" id="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
..
<select name="childselect4" ..
..until 100 and more
made it working so it changes, but only the first:
<script>
$(document).ready(function() {
//Attach onchange event to dropdownist
$('#motherselect').on('change', function() {
//Set the motherselect dropdownlist value based on childselect selected value
$('#childselect').val($('#motherselect :selected').val());
});
});
</script>

You can create a data attribute on any parent e.g. data-select-id and associate children to it using data-select-parent-id.
Wire everything-up based on those attribute selectors. No need to mess with IDs or class names.
Note: You cannot have more than one element with the same ID. They must be unique. I replaced all the id="childselect" attributes with class="childselect".
const handleParentChange = (e) => {
const
$select = $(e.target),
selectId = $select.data('select-id');
if (!selectId) return;
$(`[data-select-parent-id=${selectId}]`).val($select.val());
};
$('[data-select-id]').on('change', handleParentChange);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="motherselect" class="motherselect" data-select-id="1">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect1" class="childselect" data-select-parent-id="1">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect2" class="childselect" data-select-parent-id="1">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect3" class="childselect" data-select-parent-id="1">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
If you want the JS to work with your existing HTML, you could try this:
const handleParentChange = (e) => {
$('.childselect').val($(e.target).val());
};
$('.motherselect').on('change', handleParentChange);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="motherselect" class="motherselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect1" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect2" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect3" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
The problem with the code above is that there is no way to group the inputs given the presented HTML.
Here is a modified version where elements are logically, grouped:
const handleParentChange = (e) => {
const
$select = $(e.target),
$group = $select.closest('.groupselect');
$group.find('.childselect').val($select.val());
};
$('.motherselect').on('change', handleParentChange);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="groupselect">
<select name="motherselect" class="motherselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect1" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect2" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
<select name="childselect3" class="childselect">
<option value="0" selected="selected">none</option>
<option value="7401" required="">7401</option>
<option value="7402" required="">7402</option>
</select>
</div>

Related

Set selected option to multiple list using jQuery

I have a list from DB which is displayed in select multiple tags:
<select multiple="multiple" id="list" name="color">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
<option value="4">Magenta</option>
<option value="5">Black</option>
<option value="6">Cyan</option>
<option value="7">Yellow</option>
</select>
Given an array of values from DB, need to select only those items that are found in the DB array, for example [2,4,7]. How do I push, with jQuery, attr('selected') to these option tags?
<select multiple="multiple" id="list" name="color">
<option value="1">Red</option>
<option value="2" selected="selected">Green</option>
<option value="3">Blue</option>
<option value="4" selected="selected">Magenta</option>
<option value="5">Black</option>
<option value="6">Cyan</option>
<option value="7" selected="selected">Yellow</option>
</select>
Like above.
You've just to use the .val() method to achieve that :
$('#list').val([2,4,7]);
$('#list').val([2,4,7]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple="multiple" id="list" name="color">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
<option value="4">Magenta</option>
<option value="5">Black</option>
<option value="6">Cyan</option>
<option value="7">Yellow</option>
</select>
For the sake of completeness, I thought I would add a plain javascript solution.
Here's what I came up with, using an array, a loop, document.querySelector and setAttribute:
var preSelected = [2,4,7];
for (var i = 0; i < preSelected.length; i++) {
var option = document.querySelector('#list option[value="' + preSelected[i] + '"]');
option.setAttribute('selected','selected');
}
<select multiple="multiple" id="list" name="color">
<option value="1">Red</option>
<option value="2">Green</option>
<option value="3">Blue</option>
<option value="4">Magenta</option>
<option value="5">Black</option>
<option value="6">Cyan</option>
<option value="7">Yellow</option>
</select>

If one option Selected disabled other selected option form all select

I need to disable same other option if one option selected. i have tried but not workig for me. bellow are my html select options and want to disable if one selected
<select class="input_slip_no form-control" name="slip_no[]" id="ID_2_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_3_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_4_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_5_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
This js code I have tried but not working
$('.input_slip_no').change(function(){
alert('aaa');
// start by setting everything to enabled
$('select[name*="slip_no"] option').attr('disabled',false);
// loop each select and set the selected value to disabled in all other selects
$('select[name*="slip_no"]').each(function(){
var $this = $(this);
$('select[name*="slip_no"]').not($this).find('option').each(function(){
if($(this).attr('value') == $this.val())
$(this).attr('disabled',true);
});
});
});
I am trying to disable select class change function but js not working.I don't know where I wrong.
disabled working of below answer but not able to insert selected data because of disabled value. is it possible to only alert. that this value already selected.
That could be done simply like :
$('.input_slip_no').on('change', function() {
//Get selected options
var selected_options = $('.input_slip_no').map(function(){
return this.value
}).get();
//Disable the already selected options and enable others
$('.input_slip_no option').each(function(index) {
$(this).prop('disabled', $.inArray($(this).val(), selected_options) != -1);
});
});
Hope this helps.
$('.input_slip_no').on('change', function() {
var selected_options = $('.input_slip_no').map(function(){
return this.value
}).get();
$('.input_slip_no option').each(function(index) {
$(this).prop('disabled', $.inArray($(this).val(), selected_options) != -1);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_2_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_3_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_4_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>
<select class="input_slip_no form-control" name="slip_no[]" id="ID_5_slip_no" required="required">
<option value="SYS0001">SYS0001</option>
<option value="SYS0002">SYS0002</option>
<option value="SYS0003">SYS0003</option>
<option value="SYS0004">SYS0004</option>
<option value="SYS0005">SYS0005</option>
<option value="SYS0006">SYS0006</option>
<option value="SYS0007">SYS0007</option>
<option value="SYS0008">SYS0008</option>
<option value="SYS0009">SYS0009</option>
</select>

How to show a div when an option is selected [duplicate]

I'm trying to make a form that changes after the users makes a choice.
If the user select "Option A" then show content of the div with the class "option1".
I have already done this with very simple jquery(see code below or jsfiddle).
What I can't figure out is how to make it dynamic.
Here is how I would do it in my head:
Get all option values from "#select" and put into array.
Replace the content of "hideAll" function with the new array.
Make some kind of "for-each-function" that runs though the array and
makes the if stament.
A little note: The option value are always the same as the div class.
var hideAll = function() {
$('.option1, .option2, .option3').hide();
}
$('#select').on('change', function() {
hideAll();
var category = $(this).val();
console.log(category);
if (category === 'option1') {
$('.option1').show();
}
if (category === 'option2') {
$('.option2').show();
}
if (category === 'option3') {
$('.option3').show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="post">
<label for="option">Options</label>
<select name="select" id="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<div class="option1" style="display:block;">
<label for="countries">Countries</label>
<select name="countries">
<option value="denmark">Denmark</option>
<option value="norway">Norway</option>
<option value="uk">UK</option>
</select>
</div>
<div class="option2" style="display:none;">
<label for="letters">Letters</label>
<select name="letters">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
</div>
<div class="option3" style="display:none;">
<label for="numbers">Numbers</label>
<select name="numbers">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</form>
Give your option divs another class
<form method="post">
<label for="option">Options</label>
<select name="select" id="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<div class="option1 optiondiv" style="display:block;">
<label for="countries">Countries</label>
<select name="countries">
<option value="denmark">Denmark</option>
<option value="norway">Norway</option>
<option value="uk">UK</option>
</select>
</div>
<div class="option2 optiondiv" style="display:none;">
<label for="letters">Letters</label>
<select name="letters">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
</div>
<div class="option3 optiondiv" style="display:none;">
<label for="numbers">Numbers</label>
<select name="numbers">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
Use your new class in hideall
var hideAll = function() {
$('.optiondiv').hide();
}
Use the select value to display a block
$('#select').on('change', function() {
hideAll();
var category = $(this).val();
$('.' + category).show();
});
http://jsfiddle.net/yd5qsquL/
No need for a loop at all, and you can just search for any div whose class begins with option:
var hideAll = function() {
$('div[class^=option]').hide();
}
$('#select').on('change', function() {
hideAll();
var category = $(this).val();
$('.' + category).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="post">
<label for="option">Options</label>
<select name="select" id="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<div class="option1" style="display:block;">
<label for="countries">Countries</label>
<select name="countries">
<option value="denmark">Denmark</option>
<option value="norway">Norway</option>
<option value="uk">UK</option>
</select>
</div>
<div class="option2" style="display:none;">
<label for="letters">Letters</label>
<select name="letters">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
</div>
<div class="option3" style="display:none;">
<label for="numbers">Numbers</label>
<select name="numbers">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
I think there's an easier way to tackle this. First, use a combination of IDs and classes for the HTML, and then use JQuery to handle the dynamic stuff for you.
The HTML
Note that I have added class="option-grouping" id="option1" etc to each of your divs. This is better semantic structure and makes the JQuery easier.
<form method="post">
<label for="option">Options</label>
<select name="select" id="select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<div class="option-grouping" id="option1" style="display:block;">
<label for="countries">Countries</label>
<select name="countries">
<option value="denmark">Denmark</option>
<option value="norway">Norway</option>
<option value="uk">UK</option>
</select>
</div>
<div class="option-grouping" id="option2" style="display:none;">
<label for="letters">Letters</label>
<select name="letters">
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
</div>
<div class="option-grouping" id="option3" style="display:none;">
<label for="numbers">Numbers</label>
<select name="numbers">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
The JQuery
This is the cool part. With that all done, try this as your JQuery:
$('#select').on('change', function() {
var category = $(this).val();
/* Hide all the divs */
$('.option-grouping').hide();
/* Now unhide the selected options */
$('#' + category).show();
});
Works a treat!

Add required to select but it have already one value

I'd like to have a requiered Select for the "Brand car", the attribute is added but the problem is that I have already one value "Choose the brand name"
how can I make this work? (for the HTML5 checking)
$("#category").change(function() {
var category = parseInt($("#category").val(), 10);
switch (category){
case 2: //car
$("#car_part").show("slow");
$("#brand_car").prop('required',true);
}
....
}
<select name="category" id="category">
<option value="0">Choose the catecory</option>
<option value='2' id='cat2' > Cars </option>
<option value='88' id='cat88' > Trucks </option>
</select>
<select id="brand_car" name="brand_car">
<option id="0">Choose the brand name</option>
<option id="10" title="Audi">AUDI</option>
<option id="16" title="BMW">BMW</option>
</select>
You can add empty value to the select so it will be required if he chose "Choose the brand name"
the code will be like that:
<select name="category" id="category">
<option value="0">Choose the catecory</option>
<option value='2' id='cat2' > Cars </option>
<option value='88' id='cat88' > Trucks </option>
</select>
<select id="brand_car" name="brand_car">
<option value="" >Choose the brand name</option>
<option id="10" title="Audi">AUDI</option>
<option id="16" title="BMW">BMW</option>
</select>
How about this??
$("#category").change(function() {
var category = parseInt($("#category").val(), 10);
switch (category){
case 2: //car
$("#car_part").show("slow");
$("#brand_car").prop('required',true);
break;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<select name="category" id="category">
<option value="0">Choose the catecory</option>
<option value='2' id='cat2' > Cars </option>
<option value='88' id='cat88' > Trucks </option>
</select>
<select id="brand_car" name="brand_car">
<option selected="true" style="display:none;">Choose the brand name</option>
<option id="10" title="Audi">AUDI</option>
<option id="16" title="BMW">BMW</option>
</select>
<!--If you want to display default text as option but want to disable it below will work-->
<select id="brand_car" name="brand_car">
<option selected disabled>Choose the brand name</option>name</option>
<option id="10" title="Audi">AUDI</option>
<option id="16" title="BMW">BMW</option>
</select>

Control Multiple dropdown box by one main dropdown box

I am trying to create a main drop down box which control multiple drop down box. This multiple drop down box created in loop so i need dynamic script which works on class or id.
example -
<form method="post" name="postage">
Select All
<select id="options1" name="se1" >
<option value="0" >-- Select --</option>
<option value="Royal Mail">Royal mail</option>
<option value="USP courier">USP courier</option>
<option value="Standard courier">Standard courier</option>
<option value="DHL courier">DHL courier</option>
</select>
Select Manually-
<select id="options2" name="se2" >
<option value="0" >-- Select --</option>
<option value="Royal Mail">Royal mail</option>
<option value="USP courier">USP courier</option>
<option value="Standard courier">Standard courier</option>
<option value="DHL courier">DHL courier</option>
</select>
<select id="options3" name="se3" >
<option value="0" >-- Select --</option>
<option value="Royal Mail">Royal mail</option>
<option value="USP courier">USP courier</option>
<option value="Standard courier">Standard courier</option>
<option value="DHL courier">DHL courier</option>
</select>
<select id="options3" name="se4" >
<option value="0" >-- Select --</option>
<option value="Royal Mail">Royal mail</option>
<option value="USP courier">USP courier</option>
<option value="Standard courier">Standard courier</option>
<option value="DHL courier">DHL courier</option>
</select>
<input type="submit" name="submit_result" />
</form>
I found one script but i don't know where to change -
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
$('.west-yorkshire').change(function({
var value = $(this).val();
$('.className').each(function(
$(this).val(value);
alert(value);
));
}));
}//]]>
</script>
Is this what you were trying to achieve ? http://jsfiddle.net/9R9DV/2/
Changed some JS code
$(document).ready(function(){
$('#options1').change(function(){
var value = $(this).val();
$('.manual').each(function(){
$(this).val(value);
});
});
});
and classes to HTML code
<select class="manual" id="options2" name="se2" >
...
<select class="manual" id="options3" name="se3" >
...
etc.

Categories

Resources