fawad
fawad

Reputation: 1353

select option by value in jquery

I have 4 options:

<input type="radio" name="tag" value="1"/>
<input type="radio" name="tag" value="2"/>
<input type="radio" name="tag" value="3"/>
<input type="radio" name="tag" value="4"/>

I have written a previous page function in jquery which returns the option already selected :

var option_selected = could be 1,2,3 or 4;

$('#previous').click(function (){

});

Please tell me how is that possible when I click this previous button the option already selected should be selected. What should I add in this function to do so?

Upvotes: 2

Views: 245

Answers (3)

conheocon25
conheocon25

Reputation: 1

You can try it!

$('input:[name="tag"],[name="radio"],[value="' + option_selected + '"]').prop('checked', true);

Upvotes: 0

chaos
chaos

Reputation: 124277

$('input:radio[name="tag"][value="' + option_selected + '"]').prop('checked', true);

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

Try this:

$('input:radio[name="tag"][value="' + option_selected + '"]').prop('checked', true);

Read about .prop()

A little explanation:

$('input:radio[name="tag"][value="' + option_selected + '"]') select the input[type=radio] with name=tag and has value equal to option_selected.

Upvotes: 2

Related Questions