views:

50

answers:

3

hello, I'm trying to get the selected option via jQuery everytime I change it, and eventually the value attribute, buy I always have problems with js!!

HTML

<select>
 <option value="us">United States</option>
 <option value="gb">United Kingdom</option>
 <option value="de" selected="selected">Germany</option>
 <option value="fr">France</option>
</select>

jQuery

$("select").change (function () {
 // what can I do there?
});
+2  A: 

You can use:

$("select").change (function () {
  var selectedValue = $("select").val();

  var selectedText = $("select:selected").text();
});
kgiannakakis
Since the value of a select is the value of the selected option, you don't need the ":selected" -- $("select").val() is sufficient.
JacobM
You are right - I've corrected it.
kgiannakakis
You would need a space between the `select` and the `:selected`. It is the `option` element that can have the `:selected` pseudo-class.
bobince
+1  A: 

$("select").val() will always give you the value of the selected option at any point.

JacobM
+2  A: 
$('select').change(function() {
    alert($(this).val());
});
bobince