\r\n

\r\n\r\n\r\n\r\n\r\n\r\njavascript\r\n\r\n\r\nconst button = document.getElementsByName('button')\r\nconst time = document.getElementsByName('timer')\r\nlet counter = 0\r\nconst interval = setTimeout(()=>{\r\n counter++\r\n counter==1?time.content=`Ha pasado ${counter} minuto`:\r\n time.content=`Han pasado ${counter} minutos`\r\n},1000)\r\n\r\nbutton.addEventListener('click',()=>{\r\n clearInterval(interval)\r\n console.log('Eres el dueño del tiempo')\r\n})","answerCount":2,"upvoteCount":0,"suggestedAnswer":[{"@type":"Answer","text":"Try changing the variable name button as it maybe causing an error\nlet it be\nconst b = document.getElementsByName('button');\nand then\nb.addEventListener(........","upvoteCount":7},{"@type":"Answer","text":"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","upvoteCount":2}]} }
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 Respostas
+ 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