Home > Mobile >  Is there a short way for adding mouse click sound every time mouse clicked in my project?
Is there a short way for adding mouse click sound every time mouse clicked in my project?

Time:03-15

There are so many buttons, I can add every one js function by adding a class but is there a short way?

Here is my js function:

 function MouseSound() {

    var fileUrl = siteUrl   "audio/audio_click.mp3";
    var audio = new Audio(fileUrl);
    audio.play();
}

$('.mouse-click').click(function () {
    MouseSound();
});

Of course, this will only work for the buttons I add as a class, I want it to work for all click events. Any suggestions?

CodePudding user response:

You can use event listeners:

function MouseSound() {
    var fileUrl = siteUrl   "audio/audio_click.mp3";
    var audio = new Audio(fileUrl);
    audio.play();
}

window.addEventListener('click', MouseSound , false);

CodePudding user response:

Bind your event to the document/web page.

$(document).click(function () {
    MouseSound();
});

Or buttons only;

$(document).on("click","button", function() { MouseSound() });
  • Related