I have a multi-select that holds many sources for whence a visitor came. I am also including an option (with the value of "all") in the top of all the options.
When someone clicks that option, then all the options will be selected. And that part is easy. I've included the multi-select and jquery that listens for a click event.
<select name="sourceid[]" id="sourceid" style="width:230px;" multiple="multiple">
<option value="all">-- Select All Sources --</option>
<option value="1">Internet</option>
<option value="2">Radio</option>
<option value="3">Phone</option>
<option value="4">Other</option>
</select>
$("#sourceid").click(function(){
if($(this).val()=='all'){ $("#sourceid option").attr("selected","selected"); }
});
So far it works great! The jquery will also select the top option, naturally.
Is there a way to tell jquery to exclude selecting that one option?
Use this selector
$("option[value!=all]")
All options where the attribute value is not all
, example on jsFiddle
Attribute not equal selector jQuery API
Try this using jQuery's not() selector
$("#sourceid").click(function(){
if($(this).val()=='all'){
$("#sourceid option").not(this)
.attr("selected","selected");
}
});
How about
$("#sourceid option").not(':first').attr("selected","selected");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With