'Is there a short way for adding mouse click sound every time mouse clicked in my project?
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?
Solution 1:[1]
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);
Solution 2:[2]
Bind your event to the document/web page.
$(document).click(function () {
MouseSound();
});
Or buttons only;
$(document).on("click","button", function() { MouseSound() });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Christian Schaffner |
| Solution 2 | Jerdine Sabio |
