Solved

how to check a radio button based on value

Posted on 2011-09-15
2
318 Views
Last Modified: 2012-08-14
I'm getting a value into a variable like this:

var cur_weight=$('#weight_12').val();

The only possible values are 1, 2 and 3. Radio buttons look like this:

<input type="radio" id="weight1" class="weight" name="weight" value="1">
<input type="radio" id="weight2" class="weight" name="weight" value="2">
<input type="radio" id="weight3" class="weight" name="weight" value="3">

How can I check the right radio button based on the value matching the variable? I was trying this:

$('.weight:radio[value=cur_weight]');

...but does not seem to work. I only want one radio button checked at a time, of course. Thank you!
0
Comment
Question by:bbdesign
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36545230
use : $('.weight:radio[value="' + cur_weight +'"]')

test page : http://jsfiddle.net/dahVc/1/
0
 

Author Comment

by:bbdesign
ID: 36545287
Excellent, that works. Although the full solution would be:

$('.weight:radio[value="'+cur_weight+'"]').attr("checked", true);

Need the attribute checked in addition to the selector, which I was also missing. Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question