qadenza
qadenza

Reputation: 9293

how to play sound by clicking on hyperlink

<audio id='aclick'>
  <source src="audio/click.mp3" type="audio/mpeg">
</audio>

$(document).click(function(){
    if (click == 1) {
        document.getElementById("aclick").play();
    }
});

this works fine except on hyperlink.

I need to play the sound by clicking on any tag, including hyperlink.

Upvotes: 0

Views: 397

Answers (2)

Saurin Vala
Saurin Vala

Reputation: 1928

Updated Ans :

$('a').click(function (e) {
    e.preventDefault();                   // prevent default anchor behavior
    var goTo = this.getAttribute("href"); // store anchor href

    // do something while timeOut ticks ... 
    $("#aclick")[0].play();

    setTimeout(function(){
         window.location = goTo;
    },3000);       
});

Previous ans :

call function on any click

 jQuery( 'body' )
    .click(function() {
        alert("");
        $("#aclick")[0].play();
        return false;
    });

call function on any anchor tag click

jQuery( 'a' )
    .click(function() {
        alert("");
        $("#aclick")[0].play();
        return false;
    });

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Hyperlink tries to redirect you to the href. Stop it using e.preventDefault(); and play sound

$(document).click(function(e){
    e.preventDefault();
    if (click == 1) {       
        document.getElementById("aclick").play();
    }
});

Upvotes: 0

Related Questions