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 8730055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:56:41+00:00 2026-06-13T08:56:41+00:00

I’m attempting to make several audio elements in html that will play music when

  • 0

I’m attempting to make several audio elements in html that will play music when I tell them to via javascript.

Here’s my html:

<div id="pixelbutton" onClick="musicplay1()"> </div>

<audio id="song1" src="audio/ruins.wav" autostart=false hidden=true> </audio>

And my Javascript:

function musicplay1() {
var song1 = document.getElementById("song1");
song1.setAttribute('autostart', 'true');
}

The audio is for a good reason, so please spare me the “Oh god, really!? You’re putting sound on a webpage? That’s really annoying.” comments.

Question is: How can I feasibly create a function in javascript that controls when my html audio tag plays?

I’m rather new to JavaScript, so any help is appreciated.

  • 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-13T08:56:42+00:00Added an answer on June 13, 2026 at 8:56 am

    You haven’t asked a question, but I can see a couple of things to improve:

    • autostart and hidden are boolean attributes, but that doesn’t mean you should treat them like boolean variables. If the attribute is present then it is considered on, otherwise it’s considered off. There is no attribute hidden – an <audio> element is only shown if the controls attribute is set. Additionally, you should tell the browser what type of audio you’re using. So, your audio tag should be:
    <audio id="song1" src="audio/ruins.wav" type="audio/x-wav" />
    
    • It’s bad practice to create a function that only works for one instance. You would be better off passing the 1 as a parameter, so you would have:
    <div id="pixelbutton" onClick="musicplay(1);"> </div>
    
    • Just setting the autostart attribute probably won’t make it automatically start. Instead, use the .play() method. With this and the above point, you get:
    function musicplay(id) {
        document.getElementById('song'+id).play();
    }
    
    • Finally, unless things have changed since I last checked, only IE and Firefox support WAV, Chrome does not.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.