SVK SVK - 1 month ago 5
HTML Question

Selecting "NA" value from 1st dropdown should not disabled "NA" value from 2nd dropdown

Here i'm preventing multiple selections of Same Value. Even NA value is disabled
how to prevent NA value being disabled?

Demo Link

Html code:

<label>1st dropdown</label>
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="NA">NA</option>
</select>
<br />
<hr/>
<label>2nd dropdown</label>
<select id="select2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="NA">NA</option>
</select>


Js code:

$("select").change(function()
{
$("select option").attr("disabled",""); //enable everything
//collect the values from selected;
var arr = $.map
(
$("select option:selected"), function(n)
{
return n.value;
}
);

$("select option").filter(function()
{

return $.inArray($(this).val(),arr)>-1;
}).attr("disabled","disabled");

});

Answer

You can use something like:

return ($.inArray($(this).val(),arr)>-1 && $(this).val() !== 'NA');

in your filter function.