javascript - Play an audio file on hover -


i have made script forum , wondering if guide me how browser play audio file if user hovers on class style113, has give them warning alert says "audio play" if press ok on alert should play if press cancel should not. how can achieve this? here script have far:

my script has been removed  

you can use following code:

var elems = document.getelementsbyclassname("style113"); for(i in elems) {     elems[i].addeventlistener("mouseover", function() {         if(confirm(" %%% confirmation message %%% ")) {             // %%% code play sound %%%         }     }); } 

what does:

  1. loops on elements of class style113
  2. adds event listener each element event mouseover
  3. in each event listener, creates confirm() popup (has 2 buttons, 1 confirm , 1 cancel)
  4. if confirm() method returns true (if positive button clicked), play sound

working example on jsfiddle


update per op request in comments below, can add code specific code in for loop:

document.getelementsbyclassname('style113')[x].addeventlistener("mouseover", function() {     if(confirm("audio play")) {         // %%% code play sound %%%     } }); 

i'd advise clean source code better indenting practices. also, avoid making many dom requests (e.g., repetitive document.getelementsbyclassname()) , instead caching dom requests.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -