teamo
teamo

Reputation: 441

Get selected value from a select in jQuery

I'm trying to get the selected value from a select.

<select name="customer-country">
  <option value="CA">Canada</option>
  <option value="FR">France</option>
</select >

My problem is the code given in jQuery.com do not work.

alert($("input[name=customer-country]").val());

Any clue ?

Thanks.

Upvotes: 0

Views: 46

Answers (3)

marhs08
marhs08

Reputation: 67

try select

alert($("select[name=customer-country]").val())

Upvotes: 0

aahhaa
aahhaa

Reputation: 2275

select is not an input

$('select').on(
  "change", 
  function() {
    alert($(this).val());
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select name="customer-country">
  <option value="CA">Canada</option>
  <option value="FR">France</option>
</select >

Upvotes: 0

depperm
depperm

Reputation: 10746

Change the query to something like

$('select[name=customer-country]').val();

since a select is not a input

Upvotes: 3

Related Questions