Mj Jam
Mj Jam

Reputation: 173

playing sound when the button is pressed

I am trying to make a simple code that if the user clicked the button the sound is played.

here is the code:

<script>
function play(){
$("body").append("<embed src='play.mp3' autostart='true' loop='false' width='2'    height='0'></embed>");
}
</script>

<form action="" method="post">
<input type="button" onclick="play()" value="click">
</form>

Upvotes: 0

Views: 12160

Answers (1)

Dragos Sandu
Dragos Sandu

Reputation: 664

Try this. Should work.

<!DOCTYPE html>
<html>
<head>
<script language="javascript">
function playIt()
{
   document.getElementById("embed").innerHTML="<embed src='play.mp3' autostart=true loop=false volume=100 hidden=true>";
   return true;
}
</script>
</head>
<body>
    <form action="" method="post">
        <button type="button" onclick="playIt()">play</button>
        <div id="embed"></div>
    </form>
</body>
</html>

Upvotes: 2

Related Questions