Reputation: 8629
How do I change the select option using jquery?
Like if people click on a picture, that should change the select for "total" from 10 (current value) to $25.
I tried using
$("#buy").click(function () {
$("#total").val(25);
$("#total").focus();
});
But it changes the value from 10 to 25, but the text does not show for $25.
so I have the following
<select name = "total">
<option value = "10">$10 dollars</option>
<option value = "25">$25 dollars</option>
When I click on the image, the value for "total" changes from 10 to 20, but the text still shows as "$10 dollars".
Upvotes: 0
Views: 115
Reputation: 44205
You are selecting for an id but don't have any element with that id. Change
$("#total").val(25);
to
$('[name="total"]').val(25);
And it will work just fine. Here is a Fiddle for it.
Upvotes: 3
Reputation: 7441
$('select[name="total"] option[value="25"]').attr("selected", "selected")
Upvotes: 2