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

120
Views
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 answers
Answer question

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 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!