Solved

how to check a radio button based on value

Posted on 2011-09-15
2
307 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now