Dynamic radio button value 1 checked by default

2.8k views Asked by At

I use a code to display some radio buttons, but this code and values are loaded dynamically, so I cannot add checked to the html.

The differences between the radio buttons are values.

Is there a way to let value 1 be selected by default?

Code:

    <div class="input-box">
        <strong><?php echo $this->__('Would you recommend this product to a friend?') ?></strong>
        <?php foreach ( $this->getOptions() as $option ): ?>
        <label class="recommend">
        <input type="radio" name="recommend" id="recommend_field" class="radio-gender" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' checked="checked"' ?>><?php echo $this->__($option['label']) ?></input>
        </label>
        <?php endforeach ?>
    </div>
2

There are 2 answers

0
Mitul On BEST ANSWER

Please try bellow code to select first radio button on page load

jQuery(document).ready(function(){
    jQuery("input:radio[name=recommend]:first").attr('checked', true);
})

here is the working demo http://jsfiddle.net/patelmit69/j25w4f7z/1/

4
Nikhil Batra On

Use jquery to check a radio button.Syntax for the same is:

jQuery("#radio_1").attr('checked', 'checked');