DEV Community

loading...

HELP on JS! alert box wont appear in browser

saroj poudel
newbie
・1 min read

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!');
});

Discussion (2)

Collapse
benjioe profile image
Ferrier Benjamin • 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