0

I don't understand why addEventListener is not a function? you could help me :)

Error Uncaught TypeError: button.addEventListener is not a function Line: 10 that's my error but i do not not why :/ Html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <script type="text/javascript" src="timer.js"> </script> <p name="time"></p> <input type="button" name="button" value=""> </body> </html> javascript const button = document.getElementsByName('button') const time = document.getElementsByName('timer') let counter = 0 const interval = setTimeout(()=>{ counter++ counter==1?time.content=`Ha pasado ${counter} minuto`: time.content=`Han pasado ${counter} minutos` },1000) button.addEventListener('click',()=>{ clearInterval(interval) console.log('Eres el dueño del tiempo') })

7th Jun 2018, 2:59 AM
Javier Diáz (JavierJD)
Javier Diáz (JavierJD) - avatar
2 Respuestas
+ 7
Try changing the variable name button as it maybe causing an error let it be const b = document.getElementsByName('button'); and then b.addEventListener(........
7th Jun 2018, 3:26 AM
Mohammad Amir Aqeel
Mohammad Amir Aqeel - avatar
+ 2
You should load your javascript after you have declared button element in your dom. It is always better to load scripts at the end of body element
7th Jun 2018, 3:59 AM
Gopal Gautam
Gopal Gautam - avatar