tags:

views:

16

answers:

1

I'm messing with HTML5 and audio tags for a little project I've been working on. Basically, I have an anchor tag that activates a jquery slideToggle. I want to add sound effects to it so that clicking the anchor plays one sound effect when it opens and a different sound effect when it closes. I've spent hours trying to figure out how, but my Javascript and JQuery knowledge is extremely limited. I'm trying to avoid using external players or Flash.

A: 

I finally got it working. I used the code below. I included currentTime and Pause in order to prevent the sounds from overlapping and so that they play from the beginning every time.

var toggle=true;

function play_effect1(){
  if(toggle){
    document.getElementById('close').currentTime = 0;
    document.getElementById('close').pause();
    document.getElementById('open').play();
    toggle=false;
    }

  else {
    document.getElementById('open').currentTime = 0;
    document.getElementById('open').pause();
    document.getElementById('close').play();
    toggle=true;
    }
}
Maxor127