CSS HTML radio-button

[ Solved – 5 Answers ] CSS – How to select a radio button by default?

[Solved – 5 Answers] HTML - CSS - select a radio button by default - we have some radio buttons and when we tried to be set as selected by default


we have some radio buttons and when we tried to be set as selected by default when the page is loaded. How can we do that?

HTML Code
<input type="radio" name="sel" value="" /> 

XHTML solution:

HTML Code
<input type="radio" name="sel" 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 we don’t aim for XHTML conformance, we can simplify the code to:

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

This doesn’t exactly answer the question but for anyone using AngularJS trying to achieve this, the answer is slightly different. And actually the normal answer won’t work.
our html will look pretty similar to the normal radio button:

HTML Code
<input type='radio' name='group' ng-model='mValue' value='first' />First 
<input type='radio' name='group' ng-model='mValue' value='second' /> Second 

In our controller we declared the mValue that is associated with the radio buttons. To have one of these radio buttons preselected, assign the $scope variable associated with the group to the desired

input’s value:
$scope.mValue=”second”

This makes the “second” radio button selected on loading the page

READ  How to Modify the URL without reloading the page

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment