0

I want to have different beep in my site but its doesnt work

<body>

<!-- html work-->

    <audio id="beep-07" src="../audio/beep-07.wav" autostart="false"></audio>
    <audio id="button-3" src="../audio/button-3.wav" autostart="false"></audio>
    <button onclick="playsound('beep-07')">beep-07</button> 
    <button onclick="playsound('button-3')">button-3</button> 

<!-- js work-->

<script>
    function playsound(name) {
        var sound = document.getElementById(name);
        sound.play();
        console.log(sound,name,"1");
    }
</script>

</body>

Does anyone know why it doesn't work as expected? By the way, the console log also doesn't have any output!Does anyone know why? update Oh guys i'm so sry!the problem was a simple typo in the code!thanks for your help!i'm so sorry!

yukashima huksay
  • 5,834
  • 7
  • 45
  • 78

3 Answers3

1

Try:

<button onclick="playsound('beep-07')">beep-07</button>
<button onclick="playsound('button-3')">button-3</button>

You can read about nesting quotation marks and apostrophes on this stackoverflow thread.

Community
  • 1
  • 1
ROAL
  • 1,749
  • 1
  • 14
  • 21
  • It made no difference, what do you mean?you mean i should use ' instead of " ?are you sure there is a difference between them? – yukashima huksay Aug 27 '15 at 06:34
  • Oh yeah!i understood!but the thing is in my actual code the function is not called with a button, its called in another function with one single line:playsound("beep-07") so for me that isnt the problem:)thank you for your answer and sry for the mistake i will edit the question now;) – yukashima huksay Aug 27 '15 at 06:37
  • The difference is, writing `onclick="playsound("beep-07")"` ends the whole `onclick` too soon. The browser would interpret it as `playsound(` (from the frist `"` to the next `"` encountered), which is incorrect. If you don't use it like this, then this is not an issue, of course. – ROAL Aug 27 '15 at 07:21
0

 function playsound(name) {
        var sound = document.getElementById(name);
        sound.play();
    }
<audio id="beep-07" src="http://www.soundjay.com/button/beep-07.wav" autostart="false"></audio>
    <audio id="button-3" src="../audio/button-3.wav" autostart="false"></audio>
<button onclick="playsound('beep-07')">beep-07</button> 

Try this:

  function playSound () {
        document.getElementById('beep-07').play();
    }
    <audio id="beep-07" src="http://www.soundjay.com/button/beep-07.wav"></audio>
    <button onclick="playSound()">beep-07</button>

  
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Your code is absolutely ok,But went little wrong. Try this

<button onclick="playsound('beep-07')">beep-07</button> 
<button onclick="playsound('button-3')">button-3</button> 

Thank You

Jakarea Parvez
  • 540
  • 6
  • 9