DEV Community

saroj poudel
saroj poudel

Posted on

HELP on JS! alert box wont appear in browser

new to js ... i tried my best to display alert box in browser, this are my html and js file

<!DOCTYPE html>






Document



click here


</html

var any = document.getElementsByClassName("try");
any.addEventListener('click' , () =>{
alert('you clicked!');
});

Top comments (2)

Collapse
 
benjioe profile image
Benjioe • Edited

Hello,

  • On dev.to, posts are writes in Markdown, so use ``` html around your code (markdown guide.
Collapse
 
nbilyk profile image
Nicholas Bilyk • Edited

getElementsByClassName - note the plural, this will return a node list, not a single element to add an event listener to.

You probably want getElementById and set the id on the html element instead of the class