tags:

views:

12

answers:

2

Hi All;

<select id="COLOR">
 <option value="0">RED</option>
 <option value="1">BLACK</option>
 <option value="2">BLUE</option>
 <option value="3">WHITE</option>
 <option value="4">PINK</option>
</select>

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

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

How to make ?

Thank You.

+2  A: 

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. ​

Nick Craver
@Nick please look my questions i update
Oraclee
@oraclee - I added how to do this to the answer, this works in the case of `<outgroup>` elements as well.
Nick Craver
@Nick Thank you very much, Work
Oraclee
A: 

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>
ryanulit