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

87
Views
Audio only working for one function at a time when user clicks a href tag

I have several video tags wrapped inside of a a-href tag. When the user clicks on the a href, I would like that to trigger an audio clip before being redirected to another landing page. Currently it only works for one function at a time (audio being played) and I would like it to work for all of them.

Here is my code:

HTML:

    <a href="https://www.google.com/" id="h-link" class="h-swoosh">
      <video id="h-video" width="320" height="240" autoplay loop>
        <source src="../static/h.mp4" type="video/mp4">
      </video>
    </a>

   <audio id="h-sound">
    <source src="../static/h.mp3" type="audio/mpeg">
   </audio>


   <a href="https://www.google.com/" id="s-link" class="s-swoosh">
      <video id="s-video" width="320" height="240" autoplay loop>
        <source src="../static/s.mp4" type="video/mp4">
      </video>
   </a>

   <audio id="s-sound">
     <source src="../static/s.mp3" type="audio/mpeg">
   </audio>

JS:

hSound();
sSound();



function hSound() {

 var links = document.getElementsByClassName('h-swoosh'),

 audio = document.getElementById('h-sound'),

 clickHandler = function(event) {
  var href = this.href; // cache href here

 event.preventDefault();
 event.stopPropagation();

 audio.play();

 audio.addEventListener('ended', function(){
   window.location = href; // use cached href here
 })
};

for (var i in links) {
links[i].addEventListener('click', clickHandler, true, true);
}

};



function sSound() {

var links = document.getElementsByClassName('s-swoosh'),

audio = document.getElementById('s-sound'),

clickHandler = function(event) {
 var href = this.href; // cache href here

 event.preventDefault();
 event.stopPropagation();

 audio.play();

 audio.addEventListener('ended', function(){
   window.location = href; // use cached href here
})
};

for (var i in links) {
 links[i].addEventListener('click', clickHandler, true, true);
}

};

Any help would be immensely appreciated.

about 4 years ago · Juan Pablo Isaza
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!