This page will have 16 dropdown boxes, when I select an item from each of them, I'd like it to be removed from all the others.
This is for a football lineup page, if that helps you picture the scenario.
I hope this isn't too vague and I'm clear in what I'm struggling with.
Thanks :)
I am using in the whole project: HTML, PHP, Javascript, AJAX
<select name="pl1">
<option value="1">Ben</option>
<option value="2">Jack</option>
<option value="3">James</option>
<option value="4">John</option>
</select>
<select name="pl2">
<option value="1">Ben</option>
<option value="2">Jack</option>
<option value="3">James</option>
<option value="4">John</option>
</select>
<select name="pl3">
<option value="1">Ben</option>
<option value="2">Jack</option>
<option value="3">James</option>
<option value="4">John</option>
</select>
<select name="pl4">
<option value="1">Ben</option>
<option value="2">Jack</option>
<option value="3">James</option>
<option value="4">John</option>
</select>
Prototype solution:
http://jsfiddle.net/qrhe7/
I have taken @Tokes answer and made it a little more understandable and added some jQuery to it for dropdown and option selection. I am providing this incase someone struggles with getting it implemented. I am using the most direct selectors possible so jQuery doesn't have to work so hard. I am using this logic on 68 drop downs with 68 options in it, the performance was kinda bad when I started due to jQuery and the amount of for loops I was using but now it is really snappy so I hope someone gets some use out of it. Thanks Tokes your answer helped for my situation :)
.data() = Store arbitrary data associated with the specified element and/or return the value that was set.
.not() = Remove elements from the set of matched elements.
.on() = will attach an event handler function for one or more events to the selected elements(change in this case)
.find() = Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. in this case well find the element that has been used and put in in a var named prevValue, then we check if there is a match with the other selection fields and then removes those from the selection options.
Using jQuery:
Note that
magic-select
as a class name is not a must. It could be anything, but you'll have to adjust the javascript accordingly.Javascript solution:
HTML:
Make sure you use id and not name on your select's