Chris Hansen
Chris Hansen

Reputation: 8629

how to change select with jquery

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

Answers (2)

Daff
Daff

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

Samantha Branham
Samantha Branham

Reputation: 7441

$('select[name="total"] option[value="25"]').attr("selected", "selected")

Upvotes: 2

Related Questions