Created
October 22, 2013 13:55
-
-
Save codeodor/7101231 to your computer and use it in GitHub Desktop.
What I really want is $(‘option [value :in(1,2,3)]’) to select all options whose value is 1, 2, or 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example: | |
select 2 has, say 10 options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 | |
select1_option0 should hide nothing in select2 | |
select1_option1 should hide all but select2: options 1, 2, and 3 | |
select1_option2 should hide all but select2: options 1, 2, 3, and 9 | |
select1_option3 should hide all but select2: options 5, 6, and 7 | |
select1_option4 should hide all but select2: options 4, 6, 8, and 1 | |
I could easily loop over the data, but it occurred to me a jquery selector would be much nicer. I was thinking something like: | |
$(‘option [value :in(1,2,3)]’).attr('disabled', true) | |
Any better ideas than simply looping over the data or combining into one selector something as bad as: | |
$(‘option [value=1], option [value=2], option [value=3]’).attr('disabled', true)\ | |
? |
In essence it looks about what you said, but the rules were not as complex as you might have originally imagined since I could describe them with a hash.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. So what I did is made the rules available via a hash map from select1 option values (as the key) to select2 option values (as an array). Each value in the array is a selector that will grab the right options. Basically:
Then when it comes time to select the right options, it's just:
Rather than enable and disable separately, with every change we can simple enable all options, then disable the ones that need it (or the opposite).
In the end, it still ended up like: $(‘option [value=1], option [value=2], option [value=3]’).attr('disabled', true) to jQuery, but I let the program take care of it rather than me.
I kind of want to implement the :in selector for jQuery now, because it would be much nicer than this.