Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a radio button by default? [duplicate]

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=""  />  
like image 301
jslearner Avatar asked Apr 08 '11 08:04

jslearner


People also ask

Should radio buttons have a default selection?

Give people control and align with their expectations (Good): It is better to have a selected radio button by default, given that people cannot deselect and set the button back to its original state once one has been selected. A default selection sets the correct user expectation.

How do you select a radio button dynamically?

You can use the jQuery prop() method to check or uncheck radio button dynamically such as on click of button or an hyperlink etc. The prop() method require jQuery 1.6 and above.


1 Answers

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> 
like image 73
Shakti Singh Avatar answered Sep 18 '22 21:09

Shakti Singh