0

i'm trying to use the addeventlistener fuction on a button but it is not working properly. this is my html

<main class="main">
    <div class="start" >
        <button  id="star" >Start Quiz</button>
    </div>

and this is my script

 var btn=document.getElementById("star");

btn.addEventListener("click",alert("Your time has started"))\`

the addEventlistener executes as soon as i refresh the page after that it does not run anymore.

I tried replacing the addEventListener with the onclick()function but still the same result.i dont understand what's happening.i am not getting any error as well

  • The method works properly, when [used properly](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). See also https://stackoverflow.com/q/15886272/1169519 . – Teemu Dec 09 '22 at 10:17
  • You'd better add more context to your question. – DreamBold Dec 09 '22 at 10:27

1 Answers1

1

I got your issue. Please try with this.

var btn=document.getElementById("star");

btn.addEventListener("click",function(){
  alert("Your time has started")
})
  • 1
    This is an **often-repeated** duplicate question. Please don't post answers to obvious duplicates. Instead, find the earlier question and vote to close (when you can). It's important to keeping Stack Overflow useful that we don't have thousands of copies of the same question with thousands of different answers. More in the [help]. – T.J. Crowder Dec 09 '22 at 10:23