Chicharito
Chicharito

Reputation: 1440

Jquery Dropdownlist Select Change

RED BLACK BLUE WHITE PINK

i not to want $('#COLOR').val(2);

i want $('#COLOR').val('RED');

How to make ?

Thank You.

Upvotes: 0

Views: 577

Answers (2)

ryanulit
ryanulit

Reputation: 5001

Can you make the value "RED", which will allow you to do what you want? Or do you have to keep the same HTML. For example

<option value="RED">RED</option>

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630379

To select by value, pass that into the .val(newValue) call, like this:

$('#COLOR').val('0');

This will select "RED" because it has value="0" for it's <option> element.


For the updated question: to select by text there are a few approaches, for example:

$("#COLOR").val(function() {
  return $('option', this).filter(function() { 
    return this.innerHTML == 'RED'; 
  }).attr('value');
});

You can give it a try here. ​

Upvotes: 2

Related Questions