0

I have a "From" dropdown and a "To" dropdown. Both the elements have the same set of options.

I'm trying to take off elements of the 2nd dropdown depending on whatever is selected in the first dropdown. This way I want to eliminate the possibility of ever having the same option in both the "from" and the "to" field. Here's my code:

$(document).ready(function() { $("#from").change(function() { var str = $("#from option:selected").val(); $("#to option[value=str]").remove(); console.log(str); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>From</label> <select id="from"> <option value="Onety One">11</option> <option value="Twenty Two">22</option> <option value="Thirty Three">33</option> </select> <label>To</label> <select id="to"> <option value="Onety One">11</option> <option value="Twenty Two">22</option> <option value="Thirty Three">33</option> </select>

This does not seem to be working. Can someone please help me out and let me know what could possibly be wrong with this code.

2 Answers 2

1

You have to concatenate the str variable. And you need quotes inside [value='']
Change this line:

$("#to option[value=str]").remove(); 

to this :

$("#to option[value='"+str+"']").remove(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Worked! Thank you!
0

I think it's because you're code on the option selection is a bit twisted, try this

$("#to option:selected").remove();

instead of

$("#to option[value=str]").remove();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.