Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the radio button to "checked" in Bootstrap? [duplicate]

I'm using Bootstrap buttons as a radio button. http://getbootstrap.com/javascript/#buttons

This is my current code:

<div  class="btn-group col-md-2" data-toggle="buttons">
    <label class="btn btn-gender btn-default active">
        <input type="radio" id="gender" name="gender" value="female"> Girls
    </label>
    <label class="btn btn-gender btn-default">
        <input type="radio" id="gender" name="gender" value="male"> Guys
    </label>
</div>

How can I set "male" to checked using JavaScript?

like image 323
TIMEX Avatar asked Jun 28 '14 05:06

TIMEX


People also ask

How do I keep my radio button checked?

You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .

What is the default layout of radio buttons in bootstrap?

By default, any number of checkboxes and radios that are immediate sibling will be vertically stacked and appropriately spaced with .form-check .

What radio button attribute is used to allow only one to be selected from a group?

<input type="radio"> <input> elements of type radio are generally used in radio groups—collections of radio buttons describing a set of related options. Only one radio button in a given group can be selected at the same time.


1 Answers

Since the male is a value attribute, use:

document.querySelector("input[value='male']").checked = true;

document.querySelector

like image 120
Gaurang Tandon Avatar answered Nov 08 '22 08:11

Gaurang Tandon