Skip to content

Instantly share code, notes, and snippets.

@codeodor
Created October 22, 2013 13:55
Show Gist options
  • Save codeodor/7101231 to your computer and use it in GitHub Desktop.
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.
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)\
?
@webRat
Copy link

webRat commented Oct 22, 2013

$('#select1').on('change',function(){
var optionValue = $(this).val();

Pull in rules here, they look quite complex. You'll then renableOptions and
then pass values to disableOption. It's better to go to 1 location to fix these rules
than having to chase down every option tag.

});

function disbleOptions(){
Pass in things to disable here, but this function is solely responsible for disabling options, nothing more.
}

function renableOptions(){
$('#selector2 option').attr('disabled',false);
}

@codeodor
Copy link
Author

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:

map[select1_option1] = []
map[select1_option1].push("option [value=1]")
map[select1_option1].push("option [value=2]")
map[select1_option1].push("option [value=...]") // for each option ... of course looping over the data, not typing it out

Then when it comes time to select the right options, it's just:

$(map[select1_selected_option].join(",")).attr("disabled", true);

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.

@codeodor
Copy link
Author

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