Shijin TR
Shijin TR

Reputation: 7788

querySelector() attachEvent() not working

I have a button like bellow

<input type="button" value="check" />

And,I try to alert on this button click as follows

 document.querySelector('input[type="button"]').attachEvent('onclick', function() { alert(1); }); 

But this is not working,Why?

Upvotes: 1

Views: 188

Answers (2)

Vaibhav
Vaibhav

Reputation: 1477

Use this code snippet:-

document.querySelector('input[type="button"]').addEventListener('click', function() { alert(1); });

Upvotes: 1

Related Questions