Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filtering radio buttons in jquery, comparing values

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.

like image 523
jeynon Avatar asked Feb 19 '23 03:02

jeynon


2 Answers

$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/

like image 134
Andrew Whitaker Avatar answered Feb 28 '23 22:02

Andrew Whitaker


You should change this line:

 $radios.filter('[value=ratingAnswer]').attr('checked', true);

for this one:

 $radios.filter('[value='+ratingAnswer+']').attr('checked', true);
like image 36
Nelson Avatar answered Feb 28 '23 23:02

Nelson