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

121
Visualizações
HTMLElement.focus() not working inside HTMLElement.onpointerdown

lets say i have this html file

<!DOCTYPE html>
<html>
   <head>
       <script type="application/javascript" src="./jsfile.js" defer></script>
   </head>   
   <body>
       <p>some text</p>
       <input type="text">
   <body>
</html>

and this javascript file called jsfile.js

let p=document.getElementsByTagName("p");
let input=document.getElementsByTagName("input");
p.onpointerdown=()=>{
   input[0].focus();
};

what i am trying to do here is when the pointerdown event is called on the <p> element, the input element get focused.
i tried with mouseclick event and it worked but i cant get it to work on the pointerdown event.

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

0

There are several issues here:

  1. You are not defining element variable at all
  2. You are getting a list of elements (by using document.getElementsByTagName). In that case you will get in a p variable a collection of items, and you cannot use like .focus and other things on a collection (although its possible in jquery)

Rather you should use something like this:

// This will give you a single element
const p = document.querySelector("p");
// Same thing here
let input=document.querySelector("input");

p.onpointerdown = (e) => {
   // This part is necessary here to avoid default begaviour
   // Mose browsers would fire mousedown event if this event is not canceled, therefore it will affect your focus 
   e.preventDefault(); 

   input.focus();

};
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