How to select a radio button by default?

jslearner picture jslearner · Apr 8, 2011 · Viewed 1.5M times · Source

I have some radio buttons and I want one of them to be set as selected by default when the page is loaded. How can I do that?

<input type="radio" name="imgsel"  value=""  /> 

Answer

Shakti Singh picture Shakti Singh · Apr 8, 2011

XHTML solution:

<input type="radio" name="imgsel" value="" checked="checked" />

Please note, that the actual value of checked attribute does not actually matter; it's just a convention to assign "checked". Most importantly, strings like "true" or "false" don't have any special meaning.

If you don't aim for XHTML conformance, you can simplify the code to:

<input type="radio" name="imgsel" value="" checked>