I thought I should post this here. I always tend to look back at my old code wondering how I did these things.
Handling radio buttons or checkboxes
<input type="radio" name="print_memo" value="Yes"/> Yes <input type="radio" name="print_memo" value="No" /> No <script type="text/javascript" language="javascript"> $(document).ready( function () { $('INPUT[name=print_memo][value="<?=$this->request->print_memo?>"]').attr('checked', true) /** * $this->request->print_memo will output either 'Yes' or 'No' * Here, we used INPUT[name=XXX] to select the form element */ }); |
For select boxes
<select id="country_id" name="country"> <option value="1">Philippines</option> <option value="2">Singapore</option> <option value="3">Norway</option> </select> $(document).ready( function () { $('#country_id option[value="<?=$this->request->country_id?>"]').attr('selected', 'selected'); /** * $this->request->country_id will output 1,2 or 3 * Here, we used #country_id to select the form element */ }); |
Good post, but couldn’t you just as easily do this for the select?
$(‘#country_id’).val(request->country_id?>);
Why are you willing to depend on JS for that kind of important feedback? Besides, it adds extra code.
The code above is merely demonstrating how to use selectors.