Javascript Reference - HTML DOM Input Radio Object








The Input Radio object represents an HTML <input> element with type="radio".

Example

We can access an <input> element with type="radio" by using getElementById().


<!DOCTYPE html>
<html>
<body>
Radio Button: <input type="radio" id="myRadio">
<button onclick="myFunction()">test</button>
<script>
function myFunction() {<!--   w w  w .  j ava  2  s  .c o m-->
    var x = document.getElementById("myRadio");
    x.checked = true;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

We can create an <input> element with type="radio" by using the document.createElement() method.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--   ww w  . j ava 2  s.  c o m-->
<script>
function myFunction() {
    var x = document.createElement("INPUT");
    x.setAttribute("type", "radio");
    document.body.appendChild(x);
}
</script>
</body>
</html>

The code above is rendered as follows:





Input Radio Object Properties

Property Description
autofocus Sets or gets whether a radio button can auto focus when the page loads
checked Sets or gets the checked state of a radio button
defaultChecked Returns the default value of the checked attribute
defaultValue Sets or gets the default value of a radio button
disabled Enable or disable the radio button
form Get the form that contains the radio button
name Sets or gets the name attribute of a radio button
required Sets or gets whether the radio button must be filled before submitting a form
type Get the type of the radio button
value Sets or gets the value attribute of the radio button

Standard Properties and Events

The Input Radio object supports the standard properties and events.