Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda