1

This is a much simpler and modified part of my original code.


Anytime I call an event it doesn't function, I have tried too many times. Any help would be appreciated.

   <script type="text/javascript">
    function hello ()
    {
        alert("Hello!!!");
    }
</script>
<div id="platform">
    <div id="mainOval">
        <form id="btns">
            <input type="button" value="Timer" id="timerBtn" onclick=hello()/>
            <input type="button" value="Countdown" id="ctDownBtn" onclick=hello()/>
        </form> 
    </div>
</div>

2 Answers 2

1

It should be :

onclick="hello()" instead of onclick=hello()

you could even use it as

onclick="alert('Hello')"

Sign up to request clarification or add additional context in comments.

Comments

1

Actually,

  • don't use inline on* JavaScript handlers like onclick, it's considered unsafe and hard to debug. JavaScript scripts should be in one place only, and those are the respective tags or files. Instead, use addEventListener
  • semantics-wise, use rather <button type="button"> instead of <input type="button">
  • place your non-defered <script>s in a non-blocking manner, right before your closing </body> tag. Not in head and not anywhere else.

<body>
  <div id="platform">
    <div id="mainOval">
      <form id="btns">
        <button type="button" id="timerBtn">Timer</button>
        <button type="button" id="ctDownBtn">Countdown</button>
      </form> 
    </div>
  </div>

  <script>
  function hello () {
    alert("Hello!!!");
  }
    
  document.querySelectorAll("#timerBtn, #ctDownBtn").forEach(elBtn => {
    elBtn.addEventListener("click", hello);
  });
  </script>
</body>

Additional resources:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.