I have the following code. Trying to prepopulate a radio button based on an incoming array value. It will be 1-3
<input name="rating1" type="radio" value="1" />
<input name="rating1" type="radio" value="2" />
<input name="rating1" type="radio" value="3" />
$(document).ready(function() {
//Setting up the radio buttons
var $radios = $('input:radio[name=rating1]');
//ratingAnswer is the value I need to select in the radio group. Value=2
var ratingAnswer=answersArray[oneMap].RATING;
if($radios.is(':checked') === false) {
$radios.filter('[value=ratingAnswer]').attr('checked', true);
}
This returns me nothing as it is looking for the string ratingAnswer as the value and not the variable value of ratingAnswer. I have tried value=' + ratingAnswer + ' but it doesn't like that at all. How can I inject a variable into that value area. Thanks in advance.
$radios.filter('[value="' + ratingAnswer + '"]').attr('checked', true);
If this still doesn't work, take a close look at the value of ratingAnswer
to see if it's what you expect.
Example: http://jsfiddle.net/6zAN7/1/
You should change this line:
$radios.filter('[value=ratingAnswer]').attr('checked', true);
for this one:
$radios.filter('[value='+ratingAnswer+']').attr('checked', true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With