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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:01:42+00:00 2026-05-27T06:01:42+00:00

I am new to JS and I have a pretty simple-seeming problem. I want

  • 0

I am new to JS and I have a pretty simple-seeming problem.

I want to have a small image that when clicked changes to a different image and at the same time begins playing a looped sound file. When the image is clicked a second time it changes back to the original image and also stops the sound file.

You can think of it as a button that says “start”. when “start” is clicked it loops the sound and changes to “stop” and when “stop” is clicked it goes back to start and the sound ceases playing.

I’ve gotten as far as creating none-displayed checkbox inside of a label which is a square that when checked plays sound and when unchecked stops sound. Problem is I can’t get the checkbox to change into different images with “check”, “uncheck”.

I’ve also got some code that has a link change images, but I cannot figure out how to make it play the sound.

SO basically i need to combine these two things. BUT I CAN’T figure out how. And I’ve been googling for the past two days nonstop but cannot find a clearcut and simple answer.

It may help to know that I plan on having many of these small clickable images on the same page, so the Javascript needs to be able to be light but effect all of the links or divs or whatever they are in the end.

Sorry for such a long question. Anybody?

Thanks in advance!

  • 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-05-27T06:01:42+00:00Added an answer on May 27, 2026 at 6:01 am
    <html>
        <head>
            <script type="text/javascript">
                var pos = 0
                var sId = 'sound';
    
                function change() {
                    if(pos == 0) {
                        pos = 1;
                        document.getElementById('btn').src="http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png";
    
                        var e = document.createElement('embed');
                        e.setAttribute('src','beep.mp3');
                        e.setAttribute('id',sId);
                        e.setAttribute('hidden','true');
                        e.setAttribute('autostart','true');
                        e.setAttribute('loop','true');
    
                        document.body.appendChild(e);
                    } else {
                        pos = 0;
                        document.getElementById('btn').src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png";
                        document.body.removeChild(document.getElementById(sId));
                    }
                }
            </script>
        </head>
        <body>
            <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="change()" id="btn" />
        </body>
    </html>
    

    How about that, I think it should work.

    Edit:
    Here is an OO version that should do what you need:

    <html>
        <head>
            <script type="text/javascript">
                function imageSwitch(_imgID,_imgStart,_imgStop,_soundFile) {
                    this.imgID = _imgID;
                    this.imgStart = _imgStart;
                    this.imgStop = _imgStop;
                    this.soundFile = _soundFile;
    
                    this.pos = 0;
                    this.e;
    
                    this.change = function() {
                        if(this.pos == 0) {
                            this.pos = 1;
                            document.getElementById(this.imgID).src = this.imgStop;
    
                            this.e = document.createElement('embed');
                            this.e.setAttribute('src',this.soundFile);
                            this.e.setAttribute('id','sound'+this.imgID);
                            this.e.setAttribute('hidden','true');
                            this.e.setAttribute('autostart','true');
                            this.e.setAttribute('loop','true');
    
                            document.body.appendChild(this.e);
                        } else {
                            this.pos = 0;
                            document.getElementById(this.imgID).src = this.imgStart;
                            this.e.parentNode.removeChild(this.e);
                        }
                    }
                }
            </script>
            <script type="text/javascript">
                var abc = new imageSwitch('btn1','http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png','http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png','beep.mp3');
                var def = new imageSwitch('btn2','http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png','http://www.buzzingup.com/wp-content/uploads/2011/07/stop.png','beep.mp3');
            </script>
        </head>
        <body>
            <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="abc.change()" id="btn1" />
            <img src="http://geekoffices.com/wp-content/uploads/2011/07/start-button-300x299.png" onClick="def.change()" id="btn2" />
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty simple user control that I want to bind a ScaleTransform
This should be pretty simple, but I am new at LINQ. I have a
I am pretty new to php, but I am learning! I have a simple
Basically I do have pretty simple database that I'd like to index with Lucene.
I have a web service that's pretty simple; something like this: public class LeadService
I have a pretty simple layout - an image on the right with text
I have a pretty simple problem. When program has been started and user tries
I have a situation that is pretty simple, and I'd like to know the
OK. So I have a pretty simple question: I want to be able to
I have a new items badge on a page that I want to update

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.