Question

How do I "active" a radio button that hasn't been load yet?

It's like .live() but only for radio button.

Was it helpful?

Solution

Don't bother with jQuery unless you have to. If you're adding an element to the page or if it's an element that's meant to be checked when the page first loads, just set the value right in the HTML (or whatever server-side language creates HTML output):

<input type="radio" name="myGroup" value="My Value" checked /> My Value 

If it IS being added with jQuery, I assume it's being done with .html() or .append() or whatever other method; in which case the advice still applies:

var radioValue = 'My Value';
var newRadio = '<input type="radio" name="myGroup" value="' + radioValue + '" checked />' + radioValue;

$('#myForm').append(newRadio);

OTHER TIPS

use .prop() method ( prior to jQuery 1.6)

$('#radioButtonID').prop("checked", true);

use

$('input[name=field]:eq(1)').attr('checked', 'checked');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top