kiran
kiran

Reputation: 193

Hot to get select option value based on display name?

I have the following HTML select element:

<select id="publisher" name="publisher">
  <option value="10">Google</option>
  <option value="11">Yahoo</option>
  <option value="14">Facebook</option>
  <option value="17">Hotmail</option>
</select>

How to get option value based on display name, like i want 14 based on display name Facebook using Jquery or javascript.

Upvotes: 1

Views: 2109

Answers (1)

Vitaly Dyatlov
Vitaly Dyatlov

Reputation: 1872

var currentVal = $('#publisher').val(); //will return current selected value, numeric in your case
var facebookVal = $('#publisher').val('Facebook').val(); //will select facebook as current option and will fill facebookVal with value 14

Or, if you just wondering which value is for facebook, then:

var wonderVal = $('#publisher option:contains(Facebook)').val();

Hope this helped.

Upvotes: 4

Related Questions