2

I have a two dropdown's with the list of items 2 and 6 respectively. When i select first dropdown item, the list of items in second one will be 6 and when i select other value in first dropdown, the list of items should be 5 in second dropdown,

Please find the code here:

<select id="firstdropdown">
    <option value="1">Test</option>
    <option value="2">Testing</option>
</select>
<select id="seconddropdown">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
</select>

Can anyone suggest me a solution in jquery or javascript?

2
  • 2
    Do you mean you want to select the items with values 5 or 6, or you want there to be 5 or 6 items available in the second select? Your question is not clear. Also, SO questions should include any code you have written yourself to find a solution. Commented Dec 14, 2012 at 10:16
  • When i select 'Test' all the values in second dropdown should be listed and when i select 'Testing', only 5 values should be listed in second dropdown Commented Dec 14, 2012 at 11:06

4 Answers 4

6

You can do this way, I assumed you want to show / hide last item. you can change it to your desired by giving index.

Live Demo

$('#firstdropdown').change(function(){
    if($(this).val() == "2")
        $('#seconddropdown option:eq(5)').hide();
    else
        $('#seconddropdown option:eq(5)').show();
});​
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But i could not find any difference. I need to hide the 3rd item in the list, when i select the other item value in the first dropdown. Suggest me a solution to this.
2

From my understanding you are removing a item for a second option of first drop down

 $("#firstdropdown").change(function() {
    if(this.value === "2") $("#seconddropdown option:last").remove();
    //here you are just removing last item    
})

Comments

0

Something like this :

$("#firstdropdown").change(function() {
    if(this.value === "1") $("#seconddropdown").val("6");
    else $("#seconddropdown").val("5");
})

EDIT : added a jsfiddle : http://jsfiddle.net/scaillerie/WGhLA/

1 Comment

I think OP wants list of 6 items or 5 items for seconddropdown
0

If you need to hide any item in the dropdown, you just need to change the index of 'eq'

 $('#firstdropdown').change(function(){
     if($(this).val() == "2")
        $('#seconddropdown option:eq(3)').hide(); // change the index of 'eq' 
     else
        $('#seconddropdown option:eq(3)').show();
  });​

1 Comment

Thanks. But this code does not works for me.Suggest me some solution

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.