checked first radio button as default using javascript only

Dylan Daicy Siao
<input id="a" type="radio" name="radioButton">
<input id="b" type="radio" name="radioButton">
<input id="c" type="radio" name="radioButton">
<input id="d" type="radio" name="radioButton">

Lets say I have four radio buttons and how do I make the first-child to be checked as default by using just javascript, no jQuery please.

This is what I have done but did not work. Thanks in advance.

document.querySelectorAll('[name=radioButon]:first-child').checked = true; 
Josh Crozier

The reason your example wasn't working was because .querySelectorAll() returns a NodeList of elements. You would need to iterate over each element in the list in order to set the checked property to true:

Example Here

var elements = document.querySelectorAll('[name=radioButton]:first-child');
Array.prototype.forEach.call(elements, function(el) { el.checked = true; });

Alternatively, you could also access the first matching element:

Example Here

document.querySelectorAll('[name=radioButton]')[0].checked = true;
// or..
document.querySelector('[name=radioButton]').checked = true;

As a side note, you also had a typo.

The attribute selector [name=radioButon] should have been [name=radioButton].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

checked first radio button as default using javascript only

From Dev

Radio button checked by default when using ng-repeat

From Dev

How to set checked="checked" in first radio button?

From Dev

Issue with Radio button checked in JavaScript

From Dev

How do I check if a radio button is checked using JavaScript?

From Dev

AngularJS, ngRepeat, and default checked radio button

From Dev

Default radio button is checked but styles to it not working

From Dev

Radio button checked by default - JQuery event not working

From Dev

Radio Button checked as default remains on true

From Dev

How to have a default radio button checked in Ruby?

From Dev

Ionic 2 : Radio button in form, checked by default

From Dev

Checked radio button is not showing DIV content by default

From Dev

show a field if only a certain radio button is checked

From Dev

show a field if only a certain radio button is checked

From Dev

Radio button CheckedChanged event only when checked?

From Dev

ng-model preventing checked="checked" to select radio button by default

From Dev

How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

From Dev

how to get checked radio button value in javascript

From Dev

javascript radio button return previous checked value

From Dev

JavaScript - If radio button is checked, change background color

From Dev

Using the radio button 'checked' value in HTML and PHP

From Dev

Using jQuery to check if radio button is not checked

From Dev

Using jQuery to check if radio button is not checked

From Dev

Checking if a radio button is checked serverside using PHP

From Dev

How do I get the checked radio button value using only React?

From Dev

set a radio button value based on option select using Javascript only

From Dev

Radio button default checked not working because of JQuery function

From Dev

radio button default checked value based on variable output

From Dev

Html form Radio button set to checked by Default Based on PHP variable

Related Related

  1. 1

    checked first radio button as default using javascript only

  2. 2

    Radio button checked by default when using ng-repeat

  3. 3

    How to set checked="checked" in first radio button?

  4. 4

    Issue with Radio button checked in JavaScript

  5. 5

    How do I check if a radio button is checked using JavaScript?

  6. 6

    AngularJS, ngRepeat, and default checked radio button

  7. 7

    Default radio button is checked but styles to it not working

  8. 8

    Radio button checked by default - JQuery event not working

  9. 9

    Radio Button checked as default remains on true

  10. 10

    How to have a default radio button checked in Ruby?

  11. 11

    Ionic 2 : Radio button in form, checked by default

  12. 12

    Checked radio button is not showing DIV content by default

  13. 13

    show a field if only a certain radio button is checked

  14. 14

    show a field if only a certain radio button is checked

  15. 15

    Radio button CheckedChanged event only when checked?

  16. 16

    ng-model preventing checked="checked" to select radio button by default

  17. 17

    How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

  18. 18

    how to get checked radio button value in javascript

  19. 19

    javascript radio button return previous checked value

  20. 20

    JavaScript - If radio button is checked, change background color

  21. 21

    Using the radio button 'checked' value in HTML and PHP

  22. 22

    Using jQuery to check if radio button is not checked

  23. 23

    Using jQuery to check if radio button is not checked

  24. 24

    Checking if a radio button is checked serverside using PHP

  25. 25

    How do I get the checked radio button value using only React?

  26. 26

    set a radio button value based on option select using Javascript only

  27. 27

    Radio button default checked not working because of JQuery function

  28. 28

    radio button default checked value based on variable output

  29. 29

    Html form Radio button set to checked by Default Based on PHP variable

HotTag

Archive