mercredi 31 mai 2017

select checkbox by name and check if it checked javascript

I am trying to test if checkbox is checked. I have more inputs and they have different names. I managed to select the input but I can't seem to check if it wa selected. If I add to the condition '= true' it is checked once I load the page. If I leave it just .checked it doesn't do anything

  1. Have I selected the input right? Please note I want to select it by name rather than id or class

  2. Why is it that once the page loads my condition for if statement doesn't work?


<input type="checkbox" name="name1">
<input type="checkbox" name="name2">
<input type="checkbox" name="name3">
<input type="checkbox" name="name4">


const firstInput = document.getElementsByName('1');
if (firstInput[0].checked) {
    console.log('checked');
}




Aucun commentaire:

Enregistrer un commentaire