Jimmy
Jimmy

Reputation: 12497

Jquery Google Map Places - Getting Select Option

This is my code: http://jsfiddle.net/spadez/5dqHg/29/

It was working, to grab the country value and use it as the filter for the places autosuggest. However, I changed it to use a select box instead of an input, and it no longer works.

This is the bit of code I added:

var ctryiso = $( "#country option:selected" ).text();

Is there anything wrong with this, can anyone see why my script now no longer works? I don't get anything in the console.

Upvotes: 0

Views: 128

Answers (1)

Spokey
Spokey

Reputation: 10994

You need to get the value of the select not the text inside option

var ctryiso = $( "#country" ).val();

http://jsfiddle.net/5dqHg/32/


If you have for example the following HTML

<select name="country" id="country">
  <option value="uk" selected>United Kingdom</option>
  <option value="us">United States</option>
</select>

then

var ctryiso = $( "#country option:selected" ).text();

would return United Kingdom but what you want is the value which here is uk

<select> will always take the value of the selected option so you don't have to use option as your selector

Upvotes: 1

Related Questions