I'm trying to create a function in javascript that will see what day it is and depending on a day have a certain drop box list appear. However, for some reason it won't work and I'm not sure where the syntax error is in my code.
HTML:
<html>
<head></head>
<body>
<div class="Questions">
<div id="myAnswers" style="display : none">>
<div id="A">
<p><strong>Where are you Studying</strong></p>
<select>
<option value disabled selected>Choose Location</option>
<option value="1">Answer 1</option>
<option value="1">Answer 2</option>
<option value="1">Answer 3</option>
<option value="1">Answer 4</option>
<option value="1">Answer 5</option>
<option value="1">Answer 6</option>
</select>
</div>
<div id="B" style="display : none">
<select>
<option value disabled selected>Choose Answer</option>
<option value="1">Answer 1</option>
<option value="1">Answer 2</option>
<option value="1">Answer 3</option>
<option value="1">Answer 4</option>
</select>
</div>
</div>
</div>
</body>
</html>
Javascript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var day= new Date();
var weekday= day.getDay();
$("#A","#B").hide();
function showAnswer(){
if(weekday==3 || weekday==4){
$("#B").show();
}else{
$("#A").show();
}
}
window.onload=showAnswer;
</script>
A few issues to fix before your code can work:
As noted by #junkfoodjunkie you do not need the style="display:none;"
Use new Date(), not new Day()
Use $("#A,#B").hide(), not $("#A","#B")
Feel free to liberally use jQuery since ... :)
$(function() {
var day= new Date();
var weekday= day.getDay();
$("#A,#B").hide();
function showAnswer(weekday) {
if(weekday==3 || weekday==4) {
$("#B").show();
} else {
$("#A").show();
}
}
showAnswer( weekday );
console.log( weekday );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Questions">
<div id="myAnswers">
<div id="A">
<p><strong>Where are you Studying</strong></p>
<select>
<option value disabled selected>Choose Location</option>
<option value="1">Answer 1</option>
<option value="1">Answer 2</option>
<option value="1">Answer 3</option>
<option value="1">Answer 4</option>
<option value="1">Answer 5</option>
<option value="1">Answer 6</option>
</select>
</div>
<div id="B">
<select>
<option value disabled selected>Choose Answer</option>
<option value="1">Answer 1</option>
<option value="1">Answer 2</option>
<option value="1">Answer 3</option>
<option value="1">Answer 4</option>
</select>
</div>
</div>
</div>
Related
I am trying to make depending dropdown list. Second list depends on the first one, but when I coded and used it, it shows all of the answers, like "depending" didn't even work. Like this :
$(function() {
var $cat = $("#category1"),
$subcat = $("#category2");
$cat.on("change", function() {
var _rel = $(this).val();
$subcat.find("option").attr("style", "");
$subcat.val("");
if (!_rel) return $subcat.prop("disabled", true);
$subcat.find("[rel=" + _rel + "]").show();
$subcat.prop("disabled", false);
});
});
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<form>
<div class="form-history">
<span>
<div class="form-label"><label for="employeeID">WPROWADŹ ID TAG</label></div>
<div class="form-smallmessage"><input type="text" name="employeeID" id="employee_input"></div>
</span>
<span>
<div class="form-label"><label for="DzialID">Wybierz dział</label></div>
<div class="form-smallmessage">
<select name="category1" id="category1">
<option value="">Wybierz Dział</option>
<option value="Making_TP">Making Toothpaste</option>
<option value="Making_MW">Making MouthWash</option>
<option value="Finishing_HSL">Finishing HSL</option>
<option value="Finishing_LSL">Finishing LSL</option>
<option value="Finishing_MW">Finishing MouthWash</option>
<option value="VI_TUBES">VI Tubes</option>
<option value="VI_PRINTING">VI Printing</option>
</select>
</div>
</span>
<div class="form-label"><label for="LineID">Wybierz linie</label></div>
<div class="form-smallmessage">
<select disabled="disabled" id="category2" name="category2">
<option value>Wybierz linie</option>
<!-- Making_TP -->
<option rel="Making_TP" value="Lee1">Lee 1</option>
<option rel="Making_TP" value="Lee2">Lee 2</option>
<option rel="Making_TP" value="Lee3">Lee 3</option>
<option rel="Making_TP" value="Lee4">Lee 4</option>
<option rel="Making_TP" value="Lee5">Lee 5</option>
<option rel="Making_TP" value="Lee6">Lee 6</option>
<option rel="Making_TP" value="lee7">Lee 7</option>
<option rel="Making_TP" value="ZOATEC">Zoatec</option>
<option rel="Making_TP" value="Lee9">Lee 9</option>
<!-- Making_MW -->
<option rel="Making_MW" value="MW1">Making MW 1</option>
<option rel="Making_MW" value="MW2">Making MW 2</option>
<!-- Finishing_HSL -->
<option rel="Finishing_HSL" value="HSL1">High Speed Line 1</option>
<option rel="Finishing_HSL" value="HSL2">High Speed Line 2</option>
<option rel="Finishing_HSL" value="HSL3">High Speed Line 3</option>
<option rel="Finishing_HSL" value="HSL4">High Speed Line 4</option>
<option rel="Finishing_HSL" value="HSL5">High Speed Line 5</option>
<option rel="Finishing_HSL" value="HSL6">High Speed Line 6</option>
<option rel="Finishing_HSL" value="HSL7">High Speed Line 7</option>
<!-- Finishing_LSL -->
<option rel="Finishing_LSL" value="LSLA">Low Speed Line A</option>
<option rel="Finishing_LSL" value="LSLC">Low Speed Line C</option>
<option rel="Finishing_LSL" value="LSLD">Low Speed Line D</option>
<option rel="Finishing_LSL" value="LSLE">Low Speed Line E</option>
<option rel="Finishing_LSL" value="LSLG">Low Speed Line G</option>
<option rel="Finishing_LSL" value="LSLH">Low Speed Line H</option>
<option rel="Finishing_LSL" value="LSLI">Low Speed Line I</option>
<option rel="Finishing_LSL" value="LSLJ">Low Speed Line J</option>
<!-- Finishing_MW -->
<option rel="Finishing_MW" value="MWFIN1">Mouthwash Finishing 1</option>
<option rel="Finishing_MW" value="MWFIN2">Mouthwash Finishing 2</option>
<!-- VI_TUBES -->
<option rel="VI_TUBES" value="AISA1">Aisa 1</option>
<option rel="VI_TUBES" value="AISA2">Aisa 2</option>
<option rel="VI_TUBES" value="AISA3">Aisa 3</option>
<option rel="VI_TUBES" value="AISA4">Aisa 4</option>
<option rel="VI_TUBES" value="AISA5">Aisa 5</option>
<option rel="VI_TUBES" value="AISA6">Aisa 6</option>
<option rel="VI_TUBES" value="AISA7">Aisa 7</option>
<option rel="VI_TUBES" value="AISA8">Aisa 8</option>
<option rel="VI_TUBES" value="AISA9">Aisa 9</option>
<option rel="VI_TUBES" value="AISA10">Aisa 10</option>
<!-- VI_PRINTING -->
<option rel="VI_PRINTING" value="KPG1">KPG1</option>
<option rel="VI_PRINTING" value="KPG2">KPG2</option>
<option rel="VI_PRINTING" value="KPG3">KPG3</option>
</select>
</div>
<span>
<div class="form-label"><label for="MachineID">Nazwa Maszyny</label></div>
<div class="form-smallmessage"><input type="text" name="MachineID" id="Machine_input"> </div>
</span>
<span>
<div class="form-label"><label for="DateID">Data wystąpienia problemu</label></div>
<div class="form-smallmessage"><input type="date" name="DateID" id="Date_input"></div>
</span>
<span>
<div class="form-labelbig"><label for="ProblemID">Opisz problem</label></div>
<div class="form-smallmessage"><textarea id="text" name="ProblemID" id ="problem_input" rows="10" cols="22"></textarea></div>
</span>
<span>
<input type="button" value="ZABLOKUJ" id="gobutton">
</span>
</div>
</form>
As you can see on the screen it should show only Lee1-9 instead of all answers..
Can you tell me what did I do wrong and how do I fix it? Its the first time I'm trying to do something like this.
Thanks a lot for your time.
In the part where you call .show() on the options that need to be visible, you should also make sure that you call .hide() on the others...
So do this:
$subcat.find("option").hide().filter("[rel=" + _rel + "]").show();
Applied here:
$(function() {
var $cat = $("#category1"),
$subcat = $("#category2");
$cat.on("change", function() {
var _rel = $(this).val();
$subcat.val("").find("option").attr("style", "");
if (!_rel) return $subcat.prop("disabled", true);
$subcat.find("option").hide().filter("[rel=" + _rel + "]").show();
$subcat.prop("disabled", false);
});
});
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<form>
<div class="form-history">
<span>
<div class="form-label"><label for="employeeID">WPROWADŹ ID TAG</label></div>
<div class="form-smallmessage"><input type="text" name="employeeID" id="employee_input"></div>
</span>
<span>
<div class="form-label"><label for="DzialID">Wybierz dział</label></div>
<div class="form-smallmessage">
<select name="category1" id="category1">
<option value="">Wybierz Dział</option>
<option value="Making_TP">Making Toothpaste</option>
<option value="Making_MW">Making MouthWash</option>
<option value="Finishing_HSL">Finishing HSL</option>
<option value="Finishing_LSL">Finishing LSL</option>
<option value="Finishing_MW">Finishing MouthWash</option>
<option value="VI_TUBES">VI Tubes</option>
<option value="VI_PRINTING">VI Printing</option>
</select>
</div>
</span>
<div class="form-label"><label for="LineID">Wybierz linie</label></div>
<div class="form-smallmessage">
<select disabled="disabled" id="category2" name="category2">
<option value>Wybierz linie</option>
<!-- Making_TP -->
<option rel="Making_TP" value="Lee1">Lee 1</option>
<option rel="Making_TP" value="Lee2">Lee 2</option>
<option rel="Making_TP" value="Lee3">Lee 3</option>
<option rel="Making_TP" value="Lee4">Lee 4</option>
<option rel="Making_TP" value="Lee5">Lee 5</option>
<option rel="Making_TP" value="Lee6">Lee 6</option>
<option rel="Making_TP" value="lee7">Lee 7</option>
<option rel="Making_TP" value="ZOATEC">Zoatec</option>
<option rel="Making_TP" value="Lee9">Lee 9</option>
<!-- Making_MW -->
<option rel="Making_MW" value="MW1">Making MW 1</option>
<option rel="Making_MW" value="MW2">Making MW 2</option>
<!-- Finishing_HSL -->
<option rel="Finishing_HSL" value="HSL1">High Speed Line 1</option>
<option rel="Finishing_HSL" value="HSL2">High Speed Line 2</option>
<option rel="Finishing_HSL" value="HSL3">High Speed Line 3</option>
<option rel="Finishing_HSL" value="HSL4">High Speed Line 4</option>
<option rel="Finishing_HSL" value="HSL5">High Speed Line 5</option>
<option rel="Finishing_HSL" value="HSL6">High Speed Line 6</option>
<option rel="Finishing_HSL" value="HSL7">High Speed Line 7</option>
<!-- Finishing_LSL -->
<option rel="Finishing_LSL" value="LSLA">Low Speed Line A</option>
<option rel="Finishing_LSL" value="LSLC">Low Speed Line C</option>
<option rel="Finishing_LSL" value="LSLD">Low Speed Line D</option>
<option rel="Finishing_LSL" value="LSLE">Low Speed Line E</option>
<option rel="Finishing_LSL" value="LSLG">Low Speed Line G</option>
<option rel="Finishing_LSL" value="LSLH">Low Speed Line H</option>
<option rel="Finishing_LSL" value="LSLI">Low Speed Line I</option>
<option rel="Finishing_LSL" value="LSLJ">Low Speed Line J</option>
<!-- Finishing_MW -->
<option rel="Finishing_MW" value="MWFIN1">Mouthwash Finishing 1</option>
<option rel="Finishing_MW" value="MWFIN2">Mouthwash Finishing 2</option>
<!-- VI_TUBES -->
<option rel="VI_TUBES" value="AISA1">Aisa 1</option>
<option rel="VI_TUBES" value="AISA2">Aisa 2</option>
<option rel="VI_TUBES" value="AISA3">Aisa 3</option>
<option rel="VI_TUBES" value="AISA4">Aisa 4</option>
<option rel="VI_TUBES" value="AISA5">Aisa 5</option>
<option rel="VI_TUBES" value="AISA6">Aisa 6</option>
<option rel="VI_TUBES" value="AISA7">Aisa 7</option>
<option rel="VI_TUBES" value="AISA8">Aisa 8</option>
<option rel="VI_TUBES" value="AISA9">Aisa 9</option>
<option rel="VI_TUBES" value="AISA10">Aisa 10</option>
<!-- VI_PRINTING -->
<option rel="VI_PRINTING" value="KPG1">KPG1</option>
<option rel="VI_PRINTING" value="KPG2">KPG2</option>
<option rel="VI_PRINTING" value="KPG3">KPG3</option>
</select>
</div>
<span>
<div class="form-label"><label for="MachineID">Nazwa Maszyny</label></div>
<div class="form-smallmessage"><input type="text" name="MachineID" id="Machine_input"> </div>
</span>
<span>
<div class="form-label"><label for="DateID">Data wystąpienia problemu</label></div>
<div class="form-smallmessage"><input type="date" name="DateID" id="Date_input"></div>
</span>
<span>
<div class="form-labelbig"><label for="ProblemID">Opisz problem</label></div>
<div class="form-smallmessage"><textarea id="text" name="ProblemID" id ="problem_input" rows="10" cols="22"></textarea></div>
</span>
<span>
<input type="button" value="ZABLOKUJ" id="gobutton">
</span>
</div>
</form>
change the following code
$subcat.find("option").attr("style", "");
to
$subcat.find("option").attr("style", "display:none;");
I'm trying to create a dynamic dependent drop down lists. This code below work fine with 2 dependency and I’m trying to add a third dependency. Have you got any idea ?
For exemple :
Status 1 = offer 1, offer 2, offer 3 , offer 4
Offer 1 = Option 1
Thanks for your help
UPDATE 11.15.09
Finally find a solution
$("#street").val([]);
$('#street option').hide();
$("#city").on("change", function() {
$('#street option')
.hide() // hide all
.filter('[value^="' + $(this).val() + '"]') // filter options with required value
.show(); // and show them
$("#street").val([]);
})
$("#number").val([]);
$('#number option').hide();
$("#street").on("change", function() {
$('#number option')
.hide() // hide all
.filter('[value^="' + $(this).val() + '"]') // filter options with required value
.show(); // and show them
$("#number").val([]);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
City:
<select id="city" name="city">
<option value="0">Select City</option>
<option value="1">Manchester</option>
<option value="2">Leicester</option>
<option value="3">Londra</option>
</select>
Street:
<select id="street" name="street">
<option value="1.A">Street A</option>
<option value="1.B">Street B</option>
<option value="1.C">Street C</option>
<option value="2.D">Street D</option>
<option value="2.E">Street E</option>
<option value="2.F">Street F</option>
<option value="3.G">Street G</option>
<option value="3.H">Street H</option>
</select>
Number:
<select id="number" name="number">
<option value="1.A">1</option>
<option value="1.B">2</option>
<option value="1.C">3</option>
<option value="2.D">4</option>
<option value="2.E">5</option>
</select>
I fixed the problem with javascript. (no need to use jQuery)
Please try to use updated code below.
HTML:
<div>
<select name="select1" id="select1">
<option value="1" data-sync="1">Status 1</option>
<option value="2" data-sync="2">Status 2</option>
</select>
</div>
<div>
<select name="select2" id="select2">
<option value="1">offer 1</option>
<option value="1">offer 2</option>
<option value="1">offer 3</option>
<option value="1">offer 4</option>
<option value="1">offer 5</option>
<option value="2">offer 6</option>
<option value="2">offer 7</option>
<option value="2">offer 8</option>
<option value="2">offer 9<option>
</select>
</div>
<div>
<select name="select3" id="select3">
<option value="1">option 1</option>
<option value="2">option 2</option>
<option value="1">option 3</option>
<option value="2">option 4</option>
</select>
</div>
Javascript:
document.getElementById("select1").onchange=function() {
document.getElementById("select2").value=this.options[this.selectedIndex].getAttribute("data-sync");
document.getElementById("select3").value=this.options[this.selectedIndex].getAttribute("data-sync");
}
document.getElementById("select1").onchange();
Check the running code link below.
http://jsfiddle.net/f97u5nLa/33/
I am attempting to select a dropdown option (out of several dropdowns on the page) by its value. I'm posting below code I think is relevant but if more is needed please let me know!
HTML
<form class="controls" id="Filters">
<fieldset>
<select id="select1" class="dropdowns">
<option selected="selected" value=".default1">Default 1</option>
<option value=".option1">Option 1</option>
<option value=".option2">Option 2</option>
</select>
</fieldset>
<fieldset>
<select id="select2" class="dropdowns">
<option selected="selected" value=".default2">Default 2</option>
<option value=".option3">Option 3</option>
<option value=".option4">Option 4</option>
</fieldset>
<fieldset>
<select id="select3" class="dropdowns">
<option selected="selected" value=".default3">Default 3</option>
<option value=".option5">Option 5</option>
<option value=".option6">Option 6</option>
</fieldset>
</form>
jQuery
var example = ".option3";
var example = ".option3";
$('#Filters').find('.dropdowns').val(example).click();
<form class="controls" id="Filters">
<fieldset>
<select id="select1" class="dropdowns">
<option selected="selected" value=".default1">Default 1</option>
<option value=".option1">Option 1</option>
<option value=".option2">Option 2</option>
</select>
</fieldset>
<fieldset>
<select id="select2" class="dropdowns">
<option selected="selected" value=".default2">Default 2</option>
<option value=".option3">Option 3</option>
<option value=".option4">Option 4</option>
</select>
</fieldset>
<fieldset>
<select id="select3" class="dropdowns">
<option selected="selected" value=".default3">Default 3</option>
<option value=".option5">Option 5</option>
<option value=".option6">Option 6</option>
</select>
</fieldset>
</form>
enter code here $('#Filters').find('.dropdowns').val(example).click();
This works, selects the correct dropdown option and the dropdown initiates the filtering process (I'm using MixItUp), but it deselects all other dropdowns making them blank instead of their default values, which should be (in this example) Default 1, Default 2, Default 3, etc. (except of course the dropdown that contains the correct value).
I'm hoping to have jQuery code find the dropdown option with the correct value (example = ".option3") but not affect the other dropdowns and make sure they're still on their default initial values.
I haven't been able to find answers to my problem anywhere so any help would be appreciated!
If I got your point you can use .find('option[value="'+example+'"]').prop('selected', true);
Demo
var example = ".option3";
$('#Filters').find('option[value="'+example+'"]').prop('selected', true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="controls" id="Filters">
<fieldset>
<select id="select1" class="dropdowns">
<option selected="selected" value=".default1">Default 1</option>
<option value=".option1">Option 1</option>
<option value=".option2">Option 2</option>
</select>
</fieldset>
<fieldset>
<select id="select2" class="dropdowns">
<option selected="selected" value=".default2">Default 2</option>
<option value=".option3">Option 3</option>
<option value=".option4">Option 4</option>
</fieldset>
<fieldset>
<select id="select3" class="dropdowns">
<option selected="selected" value=".default3">Default 3</option>
<option value=".option5">Option 5</option>
<option value=".option6">Option 6</option>
</fieldset>
</form>
find does not deselect your selects. val does.
What happens is, $('#Filters') finds the form object. Then .find('.dropdowns') finds all the select objects within it. Then .val(example) sets the value of all of those select objects to .option3. However, that value happens to be invalid for most of them.
What I assume you want is to find only the select objects that actually can take your value, and set the value for those only:
var example = ".option3";
$('#Filters').find('.dropdowns:has(option[value="' + example + '"])').val(example);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="controls" id="Filters">
<fieldset>
<select id="select1" class="dropdowns">
<option selected="selected" value=".default1">Default 1</option>
<option value=".option1">Option 1</option>
<option value=".option2">Option 2</option>
</select>
</fieldset>
<fieldset>
<select id="select2" class="dropdowns">
<option selected="selected" value=".default2">Default 2</option>
<option value=".option3">Option 3</option>
<option value=".option4">Option 4</option>
</select>
</fieldset>
<fieldset>
<select id="select3" class="dropdowns">
<option selected="selected" value=".default3">Default 3</option>
<option value=".option5">Option 5</option>
<option value=".option6">Option 6</option>
</select>
</fieldset>
</form>
(Hopefully your option values won't have crazy things like quotes inside them.)
Please find below solution, I hope this will help you.
var example = ".option3";
$('#Filters').find('.dropdowns').val(example).prop('selected',true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="controls" id="Filters">
<fieldset>
<select id="select1" class="dropdowns">
<option selected="selected" value=".default1">Default 1</option>
<option value=".option1">Option 1</option>
<option value=".option2">Option 2</option>
</select>
</fieldset>
<fieldset>
<select id="select2" class="dropdowns">
<option selected="selected" value=".default2">Default 2</option>
<option value=".option3">Option 3</option>
<option value=".option4">Option 4</option>
</select>
</fieldset>
<fieldset>
<select id="select3" class="dropdowns">
<option selected="selected" value=".default3">Default 3</option>
<option value=".option5">Option 5</option>
<option value=".option6">Option 6</option>
</select>
</fieldset>
</form>
You need to be more specific when selecting your dropdown.
Use the dropdown ID to select it, then choose a value using val(), then trigger a click event, ei.:
var example = ".option3"; $('#Filters').find('.dropdowns #select2 ').val(example).click();
Otherwise you're assigning the value "option3" for all three dropdowns, which is why #select1 and #select3 look empty.
I am look for a way to make it so that when the user selects an item form all the select (for which the value of the option is not nil) the div with id "js-market" is shown.
This is my html code:
<form class="js-chose">
<div class="col-md-6 col-xm-12">
<div class="form-group js-select">
<label>Select a Market</label>
<select class="form-control">
<option value='' disabled selected>Chose a Market</option>
<%Item.all.each do |item|%>
<option value="<%=item.id%>"><%=item.name.capitalize%></option>
<%end%>
</select>
</div>
</div>
<div class="col-md-6 col-xm-12">
<div class="form-group">
<label>Enter Market with:</label>
<select class="form-control js-select">
<option value='' disabled selected>Chose a Company</option>
<%Company.where(user_id: current_user.id).each do |company|%>
<option value="<%=company.id%>"><%=company.name%></option>
<%end%>
</select>
</div>
</div>
</form>
In short I need to make jQuery show the div with id="js-market" when both selects have been filled by the user and get the values of the selected items and assign them to a variable.
Thank you. Please let me know if I wasn't clear enough.
Example that works with multiple select elements
Link to jsfiddle
var counter = $('.select').length;
$('.select').change(function() {
$.each($('.select'),function(i,e) {
var t = $(this);
if (t.val() == undefined || t.val() == '') {
$('#js-market').fadeOut();
return;
}
if (i+1 == counter) {
$('#js-market').fadeIn();
}
});
});
#js-market {
display: none;
}
<select class="select">
<option value="">No Value</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
<option value="value1">Value 4</option>
</select>
<select class="select">
<option value="">No Value</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
<option value="value1">Value 4</option>
</select>
<select class="select">
<option value="">No Value</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
<option value="value3">Value 3</option>
<option value="value1">Value 4</option>
</select>
<div id="js-market">Lorem ipsum</div>
See this fiddle
You can do this by onchange() event in javascript. In the fiddle specified above, what I do is that, I've placed 2 select boxes and a div with id demo will be shown only if two of the select boxes are selected.
HTML
<select id="mySelect1" onchange="myFunction()">
<option value="0">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="mySelect2" onchange="myFunction()">
<option value="0">--Select--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="demo"></div>
JS
function myFunction() {
if (document.getElementById("mySelect1").value > 0 && document.getElementById("mySelect2").value > 0) {
document.getElementById("demo").style.display = "block";
} else document.getElementById("demo").style.display = "none";
}
I am trying to have certain options of a dropdown display/hide when an option from a proceeding dropdown menu is selected, a friend sent me this sort of format for the script but i cant figure out how to get it to work as im not the most experienced with jquery and javascript but this worked for him (he was using text links instead of options in a dropdown)
HTML
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="location" class="col-sm-4 control-label">Choose a Location</label>
<div class="col-sm-8">
<select id="selection" class="form-control">
<option selected="selected" value="none">None</option>
<option value="Dubai">Dubai</option>
<option value="bora">Bora Bora</option>
<option value="vancouver">Vancouver</option>
<option value="rio">Rio De Janeiro</option>
</select>
</div><br></br>
<label for="length" class="col-sm-4 control-label">Choose a Resort</label>
<div class="col-sm-8">
<select class="form-control">
<option id="none">None</option>
<option class="location dubai">Resort 1</option>
<option class="location dubai">Resort 2</option>
<option class="location bora">Resort 3</option>
<option class="location bora">Resort 4</option>
<option class="location vancouver">Resort 5</option>
<option class="location rio">Resort 6</option>
</select>
</div><br></br>
SCRIPT
<script src="assets/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("#selection").change(function(){
var selection = $(this).val();
if(selection == "none"){
$("#none").find(".location").show();
}else{
$("#none").find(".location").not("."+selection).hide();
$("."+selection).show();
}
});
});
</script>
Here Is the complete code
HTML
<select id="firstSelect">
<option value="0" selected="selected">None...</option>
<option value="dubai">Dubai</option>
<option value="bora">Bora Bora</option>
<option value="vancouver">Vancouver</option>
<option value="rio">Rio De Janeiro</option>
</select>
<select id="secondSelect">
<option value="0" selected="selected">None...</option>
<option class="location dubai">Resort 1</option>
<option class="location dubai">Resort 2</option>
<option class="location bora">Resort 3</option>
<option class="location bora">Resort 4</option>
<option class="location vancouver">Resort 5</option>
<option class="location rio">Resort 6</option>
</select>
JScript
$(function(){
var conditionalSelect = $("#secondSelect"),
// Save possible options
options = conditionalSelect.children(".location").clone();
$("#firstSelect").change(function(){
var value = $(this).val();
conditionalSelect.children(".location").remove();
options.clone().filter("."+value).appendTo(conditionalSelect);
}).trigger("change");
});
Here is the working example
Change the value of dubai option from Dubai to 'dubai', because it is case sensitive and make it like this:
$(document).ready(function(){
$("#selection").change(function(){
var selection = $(this).val();
if(selection == "none"){
$(".location").show();
}else{
$(".location").not("."+selection).hide();
$("."+selection).show();
}
});
})
YOu can also set a ID to the select like: <select id ="locSelect" class="form-control"> and then use the selection like this $("#locSelect .location").