3

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: the second dropdown values need to change depending on what kind of glass was selected in the first dropdown.

Look at my code, I have the 2 dropdowns with an id of glassWidthStandard and the other dropdown glassWidthTempered.

Thank you :) here is my code:

 <select id="typeOfGlass">
  <option value="15">Standard/Annealed Glass Width</option>
    <option value="20">Tempered Glass Width</option>
</select><br>


Glass Width for Standard:<br>
    <select id="glassWidthStandard">
        <option value="19">19</option>
      <option value="20">20</option>
      <option value="21">21</option>
    </select>

Glass Width for Tempered:<br>
    <select id="glassWidthTempered">
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
    </select>
6
  • 3
    but where is your jquery code? what have you tried? Consider adding a jsfiddle. Commented Jun 5, 2015 at 16:39
  • What you want to achieve is not quiet clear. As i see there are three dropdowns. Commented Jun 5, 2015 at 16:42
  • I do not know where to start, I found this: link but the parameter to filter is the options, I cannot have the same values for groups for the second dropdown. Commented Jun 5, 2015 at 16:43
  • Yes, there are 3 dropdowns, the first dropdown will select which of the 2 other remaining dropdowns should show Commented Jun 5, 2015 at 16:43
  • 1
    I'll help you out with some pseudo code: on change of #typeOfGlass, hide or show the corresponding select. Now show use your love for doing websites and we'll show you some love in return. Commented Jun 5, 2015 at 16:46

3 Answers 3

11

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/

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.