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

191
Visualizações
How to set focus on an image on page load

Is there a way to set the focus on an on page load? I have found documentation stating to use the autofocus attribute but the documentation says that attribute only applies to input, button, textarea, and select.

Thanks.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can try scrollIntoView method on window onload

window.onload=function(){
    document.getElementById("ID_OF_IMAGE").scrollIntoView();
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I think you looking for something like that:

window.addEventListener('load', function() {
    document.querySelector(".classFromImage").focus();
})
about 4 years ago · Juan Pablo Isaza Relatório

0

What you could do is search for the img element with the autofocus attribute and set the focus after the DOM is read. You also need to set the tabindex to get that working.

I would only search for img[autofocus] so that you don't mess too much around with the default behavior.

window.addEventListener('DOMContentLoaded', function() {
  let elm = document.querySelector("img[autofocus]")
  if (elm) {
    elm.focus()
  }
})
img {
  width: 100px;
  height: 100px;
  display: inline-block;
}

:focus {
  border: 4px solid red;
}
<img autofocus tabindex="0">

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