Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
JavaScript equivalent of :active in CSS

I have the script that plays audio, the problem with the below script is that it plays when mouse click is released.

<!doctype html>
<html>

<head>
  <title>Audio</title>
</head>

<body>

  <script>
    function play() {
      var audio = document.getElementById("audio");
      audio.play();
    }
  </script>

  <input type="button" value="PLAY" onclick="play()">
  <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>

</body>

</html>

I want the above script to play on mouse click (similar to active on CSS)

Any help would be appreciated. Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Listen for the mousedown event instead:

<!doctype html>
<html>
  <head>
    <title>Audio</title>
  </head>
  <body>

    <script>
      function play() {
        var audio = document.getElementById("audio");
        audio.play();
      }
    </script>

    <input type="button" value="PLAY" onmousedown="play()">
    <audio id="audio" src="https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"></audio>

  </body>
</html>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!