How to make dependable dropdown with jquery - html

I pre-populated select tag tags with option values coming from PHP variables, and the HTML looks like this:
<!-- Type -->
<select id="d1">
<option type="colours" value="Colours">Colours</option>
<option type="fruit" value="Fruit">Fruit</option>
</select>
<!-- Group -->
<select id="d2">
<option type="colours_dark" subtype="dark" value="Dark">Dark</option>
<option type="colours_light" subtype="light" value="Light">Light</option>
<option type="fruit_ripe" value="Ripe">Ripe</option>
<option type="fruit_rotten" value="Rotten">Rotten</option>
</select>
<!-- Items -->
<select id="d3" style="display: none;">
<option value="">Choose a color</option>
<option type="colours_dark" value="Black">Black</option>
<option type="colours_dark" value="Brown">Brown</option>
<option type="colours_dark" value="Grey">Grey</option>
<option type="colours_light" value="Red">Red</option>
<option type="colours_light" value="Green">Green</option>
<option type="colours_light" value="Blue">Blue</option>
<option type="fruit_ripe" value="Banana">Banana</option>
<option type="fruit_ripe" value="Apple">Apple</option>
<option type="fruit_ripe" value="Orange">Orange</option>
<option type="fruit_rotten" value="Pear">Pear</option>
<option type="fruit_rotten" value="Peach">Peach</option>
<option type="fruit_rotten" value="Mango">Mango</option>
</select>
Initially, I don't want the third dropdown to show because the values will depend on the second dropdown, so I added the following to my jQuery snippet:
var dropDown1Selected = dropDown1.find('option').filter(':selected').val();
if(dropDown3.find('option').filter(':selected').val() == '') {
dropDown3.hide();
}
Based on my HTML, how do I make the options of dropdown 2 show depending on dropdown 1, and then have dropdown 3 showing depending on dropdown 3?
Eg: If I select fruit from dropdown 1, dropdown 2 should only show Ripe, and Rotten. And if I choose Ripe, then dropdown 3 should only show Apple and Orange.
I tried the following in my jQuery snippet, but am struggling to change the dropdown options:
$('#d1').on('change', function(e) {
var val = this.value;
// check if val exists in d2 options
$("#d2 option").filter(function() {
});
}).change();

Hope this work for you, I make dropdown 3 filtered with dropdown 2 so you don't need to hide it
$(document).ready(function () {
filterOption("d1", "d2");
filterOption("d2", "d3");
})
$('#d1').on('change', function (e) {
filterOption("d1", "d2");
filterOption("d2", "d3");
})
$('#d2').on('change', function (e) {
filterOption("d2", "d3");
})
function filterOption(parent, child) {
var parent = $(`#${parent} :selected`).attr('type');
$(`#${child} option`).each(function () {
if ($(this).val() == "" || $(this).attr('type').includes(parent)) {
$(this).show();
}
else {
$(this).hide();
}
})
$(`#${child}`).val($(`#${child} option:visible:first`).val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Type -->
<select id="d1">
<option type="colours" value="Colours">Colours</option>
<option type="fruit" value="Fruit">Fruit</option>
</select>
<!-- Group -->
<select id="d2">
<option type="colours_dark" subtype="dark" value="Dark">Dark</option>
<option type="colours_light" subtype="light" value="Light">Light</option>
<option type="fruit_ripe" value="Ripe">Ripe</option>
<option type="fruit_rotten" value="Rotten">Rotten</option>
</select>
<!-- Items -->
<select id="d3">
<option value="">Choose a color</option>
<option type="colours_dark" value="Black">Black</option>
<option type="colours_dark" value="Brown">Brown</option>
<option type="colours_dark" value="Grey">Grey</option>
<option type="colours_light" value="Red">Red</option>
<option type="colours_light" value="Green">Green</option>
<option type="colours_light" value="Blue">Blue</option>
<option type="fruit_ripe" value="Banana">Banana</option>
<option type="fruit_ripe" value="Apple">Apple</option>
<option type="fruit_ripe" value="Orange">Orange</option>
<option type="fruit_rotten" value="Pear">Pear</option>
<option type="fruit_rotten" value="Peach">Peach</option>
<option type="fruit_rotten" value="Mango">Mango</option>
</select>

Related

How to trigger a function only when an option is selected in multiple <select> tags in html?

I have three <select> tags in my html code
I want to trigger a function only when all the three <select> tags have an option selected (except for the default one). How do I do that?
<p id="cln_name"><b>Colony name</b> :
<select name="colony_name" id="colony_name" style="max-width:90%;">
<option value="default" selected="selected_code">Select your colony name</option>
<option value="code_one" selected="selected_code">PCR235LK</option>
<option value="code_two" selected="selected_code">AHF21IOD</option>
</select>
</p>
<p id="cln_type"><b>Type</b> :
<select name="colony_type" id="colony_type">
<option value="default" selected="selected_type">Select your colony type</option>
<option value="ONE" selected="selected_type">I</option>
<option value="TWO" selected="selected_type">II</option>
<option value="THREE" selected="selected_type">III</option>
</select>
</p>
<p id="qrtr_no"><b>Quarter No</b> :
<select name="quarter_no" id="quarter_no">
<option value="default" selected="selected_type">Select your quarter no</option>
<option value="first" selected="selected_type">ABC/12</option>
<option value="second" selected="selected_type">DEF/34</option>
</select>
</p>
I have added a jquery code with little bit update in your html code, instead of every option called as selected I just mentioned only default one as selected, rest is you can check your code, hope it will help if I understood your requirement correctly.
$("#colony_name,#colony_type,#quarter_no").on("change", function() {
var colony_name = $("#colony_name").val();
var colony_type = $("#colony_type").val();
var quarter_no = $("#quarter_no").val();
if (colony_name != "default" && colony_type != "default" && quarter_no != "default") {
alert("call your function in this function tag"); //call your function or execution here
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="cln_name"><b>Colony name</b> :
<select name="colony_name" id="colony_name" style="max-width:90%;">
<option value="default" selected="selected_code">Select your colony name</option>
<option value="code_one">PCR235LK</option>
<option value="code_two">AHF21IOD</option>
</select>
</p>
<p id="cln_type"><b>Type</b> :
<select name="colony_type" id="colony_type">
<option value="default" selected="selected_type">Select your colony type</option>
<option value="ONE">I</option>
<option value="TWO">II</option>
<option value="THREE">III</option>
</select>
</p>
<p id="qrtr_no"><b>Quarter No</b> :
<select name="quarter_no" id="quarter_no">
<option value="default" selected="selected_type">Select your quarter no</option>
<option value="first">ABC/12</option>
<option value="second">DEF/34</option>
</select>
</p>
Delegate and use every
Vanilla JavaScript
const container = document.getElementById('container');
const selects = [...container.querySelectorAll('select')];
container.addEventListener('change',function() {
const noDefault = selects.every(sel => sel.value !='default');
console.log(noDefault);
if (noDefault) alert("Yes"); // execute if noDefault is true
});
<div id="container">
<p id="cln_name"><b>Colony name</b> :
<select name="colony_name" id="colony_name" style="max-width:90%;">
<option value="default" selected="selected_code">Select your colony name</option>
<option value="code_one" selected="selected_code">PCR235LK</option>
<option value="code_two" selected="selected_code">AHF21IOD</option>
</select>
</p>
<p id="cln_type"><b>Type</b> :
<select name="colony_type" id="colony_type">
<option value="default" selected="selected_type">Select your colony type</option>
<option value="ONE" selected="selected_type">I</option>
<option value="TWO" selected="selected_type">II</option>
<option value="THREE" selected="selected_type">III</option>
</select>
</p>
<p id="qrtr_no"><b>Quarter No</b> :
<select name="quarter_no" id="quarter_no">
<option value="default" selected="selected_type">Select your quarter no</option>
<option value="first" selected="selected_type">ABC/12</option>
<option value="second" selected="selected_type">DEF/34</option>
</select>
</p></div>
jQuery
$('#container select').on('change', function() {
const vals = $(this)
.closest('#container')
.find('select')
.map(function() {
return this.value
})
.get(); // values of selects in container
console.log(vals)
const noDefault = vals.every(val => val != 'default');
// console.log(noDefault);
if (noDefault) console.log("Yes"); // execute if noDefault is true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<p id="cln_name"><b>Colony name</b> :
<select name="colony_name" id="colony_name" style="max-width:90%;">
<option value="default" selected="selected_code">Select your colony name</option>
<option value="code_one" selected="selected_code">PCR235LK</option>
<option value="code_two" selected="selected_code">AHF21IOD</option>
</select>
</p>
<p id="cln_type"><b>Type</b> :
<select name="colony_type" id="colony_type">
<option value="default" selected="selected_type">Select your colony type</option>
<option value="ONE" selected="selected_type">I</option>
<option value="TWO" selected="selected_type">II</option>
<option value="THREE" selected="selected_type">III</option>
</select>
</p>
<p id="qrtr_no"><b>Quarter No</b> :
<select name="quarter_no" id="quarter_no">
<option value="default" selected="selected_type">Select your quarter no</option>
<option value="first" selected="selected_type">ABC/12</option>
<option value="second" selected="selected_type">DEF/34</option>
</select>
</p>
</div>

get the duplicated select

I have repeated select, like below:
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
in jquery I need to if user has select type="car" twice, any ideas?
You may combine the change event with the filter() method.
In order to disallow to select a value already selected you may use an additional data parameter for each select.
$('.select-list').on('change', function(e) {
var sel = this.value;
if ($('.select-list').filter((i,e) => e.value == sel).length > 1) {
$(this).val(this.dataset.oldSelected);
console.log("you cannot select twice...");
} else {
this.dataset.oldSelected = sel;
}
}).trigger('change');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="select-list">
<option data-type="car" selected>one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus" selected>two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck" selected>three</option>
</select>

automatically position to an option in a drop down select box

how to position to an option in a select box when opened but the initial value should be empty?
ex:
<select>
<option value="" disabled selected style="display:none"></option>//<--- Initial value
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option> // <--- Automatically points here when opened
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
</select>
http://jsfiddle.net/vcGrr/
Using Jquery it is simple enough:
$("select").focus(function(){
$("select option").filter(function() {
return $(this).text() == "2014";
}).prop('selected', true);
});
Thanks for pointing out that my fiddle did not match the code - correct fiddle is above
Did you mean..
<option value="2014" selected>2014</option>
Set id to your <select> tag
<select id="ddl">
At the end of document add javascipt
<script type="text/javascript">
var firstclick=0;
document.getElementById("ddl").onclick=function(){
if(firstclick==0){
document.getElementById("ddl").selectedIndex=11;
firstclick=1;
}
}
</script>
Here is a jQuery version: jsfiddle
Relevant HTML:
<select>
<option value="" disabled selected style="display:none"></option>
...
<option id="2014" value="2014">2014</option>
...
</select>
jQuery:
var oldValue = null;
$( function ()
{
$('select').click(function(){
var s = $(this);
val = s.val();
oldValue = val;
if( val == null )
{
document.getElementById("2014").selected = true;
}
});
});
Update
Slightly modified version that contains purely jQuery selectors is here: jsfiddle.
Only change is to set 2014 to selected:
$("#2014").prop('selected', true);
Put selected on the option you want by default to be shown as follows-
<select>
<option value=""></option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014" selected>2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
</select>
Fiddle
You can’t. Implementations of select element work so that initially the default value is shown. Moreover, if a value like “2014” were shown but the default value were actually empty, it would seriously mislead users. You should reconsider the original problem (the unspecified problem that made you want to achieve something that cannot be achieved).
The behavior you describe could be simulated using CSS and JavaScript, by constructing something that looks like a select element but has been coded entirely differently.

Hide selected options on from other select elements

Given the following HTML:
<select name="question_order[]" id="data1">
<option value="">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<select name="question_order[]" id="data2">
<option value="">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<select name="question_order[]" id="data3">
<option value="">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
How can I hide/remove the selected options in data3, if it's already selected in data1 and data2?
For example, if I select 1 in data1, then 2 in data2, how can I remove those two option on data3?
Check the Js fiddle
http://jsfiddle.net/aZPdd/
$('#data1').on('change', function() {
var val=this.value;
$("#data3").find("option:contains("+val+")").remove();
});
$('#data2').on('change', function() {
var val=this.value;
$("#data3").find("option:contains("+val+")").remove();
});
$('#data3').on('change', function() {
});

HTML Form first dropdown auto change the second dropdown options

I have this code on the first dropdown:
<td align="left" style="padding-bottom:10px">
<select name="category" id="category">
<option selected value="Please Select">Please Select</option>
<option value="Cars">Cars</option>
<option value="Trucks">Trucks</option>
<option value="Motorcycles">Motorcycles</option>
<option value="Boats">Boats</option>
</select>
</td>
and i also have a second category with different colors for the previous option.
For example:
If i select Cars the second dropdown will appear to select "Red", "Green" or "Blue" but if i select Trucks the second dropdown will appear to select "Black" or "White" options only.
Both second dropdown options will go to the same column (Subcategory) in the mySQL DB so no Car can have the value "Black" on Subcategory column or no Truck can have the value "Red" on Subcategory column.
Thanks
Working example: http://jsfiddle.net/7YeL6/4/
Given this structure:
<select name="category" id="category">
<option selected value="Please Select">Please Select</option>
<option value="Cars">Cars</option>
<option value="Trucks">Trucks</option>
<option value="Motorcycles">Motorcycles</option>
<option value="Boats">Boats</option>
</select>
<div>
<select name="category2" id="truck" class="second">
<option value="white">white</option>
<option value="black">black</option>
</select>
<select name="category2" id="car" class="second">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
</div>
You could use jQuery .change function:
$("#category").change(function () {
var str = "";
str = $("select#category option:selected").text();
if(str == "Trucks"){
$("select.second").not("#truck").hide();
$("#truck").show();
$("#truck").fadeIn(1000);
}
else if(str == "Cars"){
$("select.second").not("#car").hide();
$("#car").show();
$("#car").fadeIn(1000);
}
})
CSS
#category2{
display: none;
}