Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to check a radio button based on value

Posted on 2011-09-15
2
Medium Priority
?
341 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 83

Accepted Solution

by:
leakim971 earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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 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…
Suggested Courses

926 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