Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery select input options, except one

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?

like image 295
coffeemonitor Avatar asked May 02 '11 18:05

coffeemonitor


3 Answers

Use this selector

$("option[value!=all]")

All options where the attribute value is not all, example on jsFiddle

Attribute not equal selector jQuery API

like image 116
BrunoLM Avatar answered Oct 05 '22 10:10

BrunoLM


Try this using jQuery's not() selector

$("#sourceid").click(function(){

  if($(this).val()=='all'){ 
            $("#sourceid option").not(this)
                   .attr("selected","selected"); 
  }

});
like image 21
Naftali Avatar answered Oct 05 '22 11:10

Naftali


How about

$("#sourceid option").not(':first').attr("selected","selected");
like image 25
Frenchi In LA Avatar answered Oct 05 '22 09:10

Frenchi In LA