S L
S L

Reputation: 14318

How to select option in select list using jquery

i have a form that has select element

<select name="adddisplaypage[]" id="adddisplaypage" multiple="multiple">
    <option value="all" label="all">all</option>
    <option value="index" label="index">index</option>
    <option value="tour" label="tour">tour</option>
    <option value="aboutus" label="about us">about us</option>
    <option value="contactus" label="contact us">contact us</option>
    <option value="destination" label="destination">destination</option>
    <option value="reservation" label="reservation">reservation</option>
</select>

can anyone help me to select this option (multiple select) on click i.e the option gets selected when clicked, and deselected if selected on click.

Upvotes: 1

Views: 561

Answers (3)

Brian Rose
Brian Rose

Reputation: 1725

I realized I may have misunderstood your question. Something like the following should work, though I'm not sure about browser support:

$('#adddisplaypage option').click(function(e) {
  e.preventDefault();
  var self = $(this);

  if(self.attr('selected') == '') {
    self.attr('selected', 'selected');
  } else {
    self.attr('selected', '');
  }
});

Upvotes: 2

Brian Rose
Brian Rose

Reputation: 1725

You can pass an array to the .val() method. For instance:

$('#adddisplaypage').val(['index', 'tour']);

Upvotes: 1

slhck
slhck

Reputation: 38652

In your click() handler, you could write something like:

$("#adddisplaypage").val("index");

That should select "index", for example.

Upvotes: 2

Related Questions