Solved

how to check a radio button based on value

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

757 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

18 Experts available now in Live!

Get 1:1 Help Now