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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:01:17+00:00 2026-06-15T06:01:17+00:00

Friends, I’m basically trying to build a simple audio player that grabs the id

  • 0

Friends,

I’m basically trying to build a simple audio player that grabs the id of the image clicked and plays the corresponding audio file. Here’s where I’m at… with no luck.

I have been trying to find an elegant way to do this for hours and would be most grateful for some help!

Thanks!

<script type="text/javascript"> 
$(function() {
    $(".playback").click(function(){
        var selectedAudio = $(this).attr("id");
        var myAudio = document.getElementById(selectedAudio);
        myAudio.play();
});
</script>


<body>
    <audio id="boardroom" preload="auto"><source src="boardroom.mp3" type="audio/mpeg" /></audio>
    <audio id="bathroom" preload="auto"><source src="bathroom.mp3" type="audio/mpeg" /></audio>
    <audio id="livingroom" preload="auto"><source src="livingroom.mp3" type="audio/mpeg" /></audio>

    <img class="playback" id="boardroom" src="images/audio.png">


</body
  • 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-15T06:01:19+00:00Added an answer on June 15, 2026 at 6:01 am

    2 things.

    1. Never, ever, use the same id on more than one element in your document. They are supposed to be unique, and bad stuff can happen when they are not unique. In your case, your image and your audio element share the same id, this is BAD. How will document.getElementById know which element to find if more than one share an ID? You need to make them different, though you can still use one to find the other. <img id="somename"> and <audio id="somename-audio">, for instance.

    2. The audio source in your fiddle didn’t point to an audio file. It pointed to a webpage about the audio file. I fixed the link to point to the actual audio file.

    Working version: http://jsfiddle.net/Pzsax/2/

    Html:

    <audio id="cello-audio" preload="auto">
        <source src="http://upload.wikimedia.org/wikipedia/commons/4/43/JOHN_MICHEL_CELLO-J_S_BACH_CELLO_SUITE_1_in_G_Prelude.ogg" type="audio/mpeg" />
    </audio>
    
    <img class="playback" id="cello" src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Sound-icon.svg/50px-Sound-icon.svg.png">​
    

    JS:

    $(".playback").click(function() {
        var selectedAudio = $(this).attr("id");
        var myAudio = document.getElementById(selectedAudio + "-audio");
        myAudio.play();
    });​
    

    Or since you seem to be using jQuery, that could simply be:

    $(".playback").click(function() {
        var selectedAudio = $(this).attr("id");
        var myAudio = $('#' + selectedAudio + "-audio");
        myAudio.play();
    });​
    

    Lastly, rather than relying on id’s to pass arund meaningful data, perhaps data attributes might be a cleaner approach, allowing you to encode bits of data, like names of sounds, in the buttons that play them.

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

Sidebar

Related Questions

Friends, I am trying to processing a huge amount of audio files using a
Friends,I am trying to calculate the difference between two date&time entries that the user
Friends I have an image and I wish to move that image to right
Friends, i m trying add image to my Jbutton using seticon method but it
friends. I have a 'make'-like style file needed to be parsed. The grammar is
friends, i have created custom title bar using following titlebar.xml file with code <?xml
friends i am trying to work with predesigned visual studio dotNet projects. so i
Friends, I've a datagridview with 5 columns and 10 fixed rows. That means I've
Friends I am having a String that contains date-record String date=10-Oct-2012 @12-Oct-2012 @$12-Oct-2012 @12-Oct-2012
Friends In, testing of Apple Push Notification Service ,server sends me response .k that

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.