Jump to content

Check out our Community Blogs

Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
- - - - -

Need a simple example of running audio on clicking the image

javascript click playing audio

  • Please log in to reply
1 reply to this topic

#1 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? 

  • 0

#2 AmyGreen


    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Location:UK

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">

(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 () {

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

  • 0