Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:05:35+00:00 2026-06-05T19:05:35+00:00

I want a random sound to play on a click on a button on

  • 0

I want a random sound to play on a click on a button on a web page. I have researched it quite a bit and this discussion has helped me most: http://www.elated.com/forums/topic/5196/

One poster recommended making a Javascript function to run whenever the button is clicked, as follows:

<script>
function playSound() {
  var sounds = [
    "http://www.mysite.com/1.wav",
    "http://www.mysite.com/2.wav",
    "http://www.mysite.com/3.wav"
  ];

  **// Choose a random sound here and play it**
}
</script>

I understand the part about making an array of sounds. I think I have the part about selecting a random array element figured out. I am just stuck on how to play a sound inside the JS function as the poster recommends. Can I use an HTML5 audio tag inside the JS function?

I don’t care whether the code for actually playing the file is inside or outside the function. Actually, I was first going to use JS just to randomly select an element, then have a line of HTML play the element of the array returned by the JS function. I gave up on that because I couldn’t figure out how to specify that I wanted to play the return value of a JS function in HTML.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:05:36+00:00Added an answer on June 5, 2026 at 7:05 pm

    Use JavaScript to Add Sound

    Place the following script in the <head> of your HTML document:

    <script language="javascript" type="text/javascript">
     function playSound(soundfile) {
     document.getElementById("dummy").innerHTML=
     "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
     }
     </script>
    

    The Sound is Placed in an Empty Span

    The JavaScript places an embed tag inside an empty span tag when the script is initiated. So, you need to add the following span file somewhere within the body of your HTML page, preferabl near the top of the document:

    <span id="dummy"></span>
    

    Call the Script with an onmouseover or onclick Attribute

    The last thing you need to add is an element that you want to generate the sound on click or on mouseover. Call the script with one of those attributes:

    <a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a>
    
     <p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a random frame.origin.x and frame.origin.y on each button click. The button need's
I want to generate random colours which can be attractive in pie charts.I have
I want to generate unique random, N-valued key. This key can contain numbers and
I want to generate some random IP Address. But evertime this generateIPAddress function returns
I want to generate random datasets with regex. Therefore I have an expression like
I want to create a random password generator in my ASP.NET C# web application.
I want a random expiration time on EACH iteration. This example will only randomize
this is my first web-programming experience so I hope my questions doesn't sound very
I want to generate random numbers from -n to n excluding 0. Can someone
I want to choose random numbers within a range of numbers, but with weighting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.