Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Press enter on an image and it plays a sound?

I have a tabbable image that I want to play a sound if it is tabbed to and enter is pressed. It's not a button for a form or anything, just an image. How can this be done?

 <img src="/images/butt.gif" onclick="fartSound();" tabindex="0">

I already have Javascript where a sound is played when clicked, but I would really love it if I could also have the sound played when it is tabbed to and enter is pressed.

function fartSound(){
  var sound = new Audio('/audio/fart.mp3');
  sound.play();
};

Any ideas? Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can listen to the focus event with javascript.

Something like element.addEventListener('focus', fartSound);

Working demo:

Just click on the white space and hit tab, you will see the console log.

function fartSound(){
  console.log("Prrr");
  var sound = new Audio('/audio/fart.mp3');
  sound.play();
};


const funnyPic = document.getElementById('funnyPic');

funnyPic.addEventListener('focus', fartSound);
 <img src="/images/butt.gif" tabindex="0" id="funnyPic">

about 4 years ago · Juan Pablo Isaza Report

0

Add addEventListeners for pressing(keydown) on 'enter'. and add onFocus event in HTML to call the function on click or tabbed

function fartSound(){
  console.log('clicked');
  var sound = new Audio('/audio/fart.mp3');
  sound.play();
};


const element = document.getElementById('element');

element.addEventListener('keydown', function(e) {
    //Enter key value is 13
    if(e.which == 13) {
       fartSound();
    }
});
 <img src="/images/butt.gif" onfocus="fartSound()" tabindex="0" id="element">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!