Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need a simple example of running audio on clicking the image

javascript click playing audio

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 MarlenaLegarreta

MarlenaLegarreta

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 07 November 2016 - 10:26 PM

My requirement is to play audio if someone clicks on page images. The mp3 are directly located on my computer. This is simple, but I can't get any well know code on the internet. This means, I can have a simple link, which will open in a tab if an image is clicked using '<img src=>' with 'ahref'. But what to do, if I have to play an audio or video? 



#2 AmyGreen

AmyGreen

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 19 November 2016 - 10:08 AM

Place this is the body of your html file

<audio id="MyAudio">
    <source src="/sounds/AudioFileName.mp3" type="audio/mpeg">
</audio>

(Change URL to your audio file as needed)

 

Then add this Jquery code (inside <script> tags unless you're using a separate file)

$(document).ready(function() {
    $("img").on('click', function () {
        $('#MyAudio')[0].play();
    });
});

Now every image should play the sound file you specify in the first bit of code.