Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to check a radio button based on value

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

828 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