Reputation: 8352
I have a button that, when clicked, should get me the value of a combo box's selected item. For example, if I have the following combo and button:
<select id="client-sort-type">
<option></option>
<option>Name</option>
<option>Recent</option>
</select>
<button id="client-sort-submit" type="button">Sort</button>
I want something like...
<script type="text/javascript">
$("#client-sort-submit").click(function () {
var val = $("#client-sort-type").attr("value");
window.location.href = "Project/Index/" + val;
});
</script>
Only this isn't working, it always returns nothing. Help?
Upvotes: 0
Views: 1649
Reputation: 28837
Well for starters you should have value=
in your <options>
, otherwise it might generate unwanted behaviours.
Use <option value="somevalue" >Name</option>
If you want to get the value via jQuery you should use:
$("#client-sort-type").val(); // this will give you "somevalue"
If you want to get the text of the selected option use:
$("#client-sort-type option:selected").text(); // This will give you "Name"
Read the difference between this and this, if you keep your html as it is, it's better to use .text()
Upvotes: 1
Reputation: 3424
Instead of
val = $("#client-sort-type").attr("value");
use
val = $("#client-sort-type").val();
Upvotes: 0