Why not work for all inputов?

Learn Javascript, I want to do to when the input, displays a message to the console. My code only works for first input, still gives error on target , can explain what's wrong?
Code
March 19th 20 at 08:54
2 answers
March 19th 20 at 08:56
Solution
querySelectorAll to find all, not the first one on the page
and then for everyone to hang an event listener
https://codepen.io/anon/pen/rREMKb
@armani.Prohaska52 that's odd, did querySelectorAll , but swore to addEventListener, now everything is correct. And tell me why swears on target ? - Camylle.Murp commented on March 19th 20 at 08:59
@Camylle.Murp, and I didn't notice :)
because why calling check() at the end of the script?
it turns out that

function check(e) {
 // e = undefined
 // Cannot read property 'target' of undefined
if(e.target.checked){
console.log(e.target);
}
}

check() // don't need to call this function separately from the listener
- armani.Prohaska52 commented on March 19th 20 at 09:02
@armani.Prohaska52, thank you) - Camylle.Murp commented on March 19th 20 at 09:05
@Camylle.Murp,
done with querySelectorAll , but swore to addEventListener


see :D
document - the nurse
querySelector("boutique") - bring the boutique
querySelectorAll("boutique") - bring all the boutiques
addEventListener - devour

querySelector("boutique") will bring you one new and you can eat
querySelectorAll("boutique") will bring all the boutiques IN the PLATE, and you can't directly eat anything

therefore, it is necessary to walk through the result by forEach function to hang an event listener specifically on each element and not on the "plate"
.forEach(function(input) {
 input.addEventListener('click', check)
 });
- armani.Prohaska52 commented on March 19th 20 at 09:08
@armani.Prohaska52, better than any tutorial on JS explained)) - Camylle.Murp commented on March 19th 20 at 09:11
March 19th 20 at 08:58
For starters it would be nice to read documentay according to the method that you cause. Error on target when you call the handler manually at the end of the code (where in this case will take there event and especially to event.target?
Well, for input type="checkbox" you need to subscribe to change

Find more questions by tags JavaScript