You can use the following code:
var elems = document.getElementsByClassName("style113");
for(i in elems) {
elems[i].addEventListener("mouseover", function() {
if(confirm(" %%% CONFIRMATION MESSAGE %%% ")) {
// %%% CODE TO PLAY SOUND %%%
}
});
}
What it does:
- Loops over elements of class
style113
- Adds an event listener to each element to event
mouseover
- In each event listener, creates a
confirm()
popup (has two buttons, one to confirm and one to cancel)
- If the
confirm()
method returns true
(if the positive button is clicked), then play sound
Working example on JSFiddle
UPDATE As per OP request in the comments below, you can add this code to your specific code in the for
loop:
document.getElementsByClassName('style113')[x].addEventListener("mouseover", function() {
if(confirm("audio is about to play")) {
// %%% CODE TO PLAY SOUND %%%
}
});
I'd also advise you to clean up your source code with better indenting practices. Also, avoid making too many DOM requests (e.g., repetitive document.getElementsByClassName()
) and look instead to caching DOM requests.