Play audio when checkbox is checked with javascript -


i'm trying make short audio file play when html checkbox checked. i'm missing elementary. here code:

<body>     <input type="checkbox" id="cena" onchange="myfunction()"></input><label for="cena"></label> </label><script> function myfunction(){ var audio = new audio('rusbtaudio.mp3'); audio.play(); } </script>     </body> 

rusbtaudio.mp3 in same folder html file.

<input> element self-closing. use .load(), canplay event. substitute new audio() new audio()

<body>     <input type="checkbox" id="cena" onchange="myfunction(this)" />     <label for="cena"></label> <script>   var audio = new audio('rusbtaudio.mp3');   audio.oncanplay = function() {     if (document.getelementbyid("cena").checked) this.play()   }   function myfunction(el) {         if (el.checked) {       audio.load();     }   } </script> </body> 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -