I have my first dropdown with 2 selections: Standard Glass and Tempered glass. I have another dropdown where the user can select the width of the glass.
My problem:
I would suggest having just two selects, then changing the 2nd select based on what is selected in the first.
$('#typeOfGlass').on('change', function(){
$('#glassWidth').html('');
if($('#typeOfGlass').val()==15){
$('#glassWidth').append('<option value="19">19</option>');
$('#glassWidth').append('<option value="20">20</option>');
$('#glassWidth').append('<option value="21">21</option>');
}else{
$('#glassWidth').append('<option value="6">6</option>');
$('#glassWidth').append('<option value="7">7</option>');
$('#glassWidth').append('<option value="8">8</option>');
$('#glassWidth').append('<option value="9">9</option>');
}
});
Here is a working example: https://jsfiddle.net/6hmpa1ax/
have a look at this code
<body onLoad="fun('')">
<select id="typeOfGlass" onchange="fun(this.value)" >
<option value=''>select</option>
<option value="15">Standard/Annealed Glass Width</option>
<option value="20">Tempered Glass Width</option>
</select><br/><br/>
<select id="glassWidthStandard">
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
</select>
<select id="glassWidthTempered">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
</body>
<script>
function fun(val)
{
if(val==15)
{
$('#glassWidthTempered').hide();
$('#glassWidthStandard').show();
}
else if(val==20)
{
$('#glassWidthStandard').hide();
$('#glassWidthTempered').show();
}
else if(val=='')
{
$('#glassWidthStandard').hide();
$('#glassWidthTempered').hide();
}
}
</script>
As an alternative, if you don't want to dynamically change the DOM you can hide/show the premade markup. DEMO
HTML
<select id="typeOfGlass">
<option selected disabled>Please select a glass type</option>
<option value="15">Standard/Annealed Glass Width</option>
<option value="20">Tempered Glass Width</option>
</select>
<br />
<div class="standard">
<label>Glass Width for Standard:</label>
<select id="glassWidthStandard">
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
</select>
</div>
<div class="tempered">
<label>Glass Width for Tempered:</label>
<select id="glassWidthTempered">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
jQuery
$('#typeOfGlass').on('change', function(){
if( $(this).val() == 15 ){
$('.tempered').hide();
$('.standard').show();
} else if( $(this).val() == 20 ){
$('.standard').hide();
$('.tempered').show();
}
});
CSS
.standard, .tempered {
display:none;
}