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

382
Views
JavaScript .focus() function not working in a text field

I'm trying to use .focus() on the following form field without success. The tabindex of the div this element is in is 0.

<input class="form-control ng-pristine ng-valid ng-empty ng-touched" id="accessionNumber" type="text" name="filter.accessionNumber" ng-model="selectFields.accessionNumber" style="">

I'm using the following:

document.getElementsByName("filter.accessionNumber")[0].focus()

I've also tried document.getElementById using the "accessionNumber" ID as well as the .click() function instead of the .focus() function but none have worked so far

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Short answer: Maybe have a look at this post

Longer answer: Since you are using Angular you should use the features of the framework that enable this: ViewChild

Steps to make it work:

  1. In your component create a variable with the ViewChild annotation
  2. Mark the element with the ViewChild id in the Components Template
  3. Access your element in ngAfterViewInitlike
this.myElement.nativeElement.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!