Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda