Reputation: 1440
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
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
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');
});
Upvotes: 2