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

133
Vistas
Probem with the focus function in Angular

I hope you can help me, I just started as a programmer and I have a newbie problem, I have two inputs, I want to write a series of numbers in my first input (DO) and then automatically pass to the second input (communal code).

Inputs:

two input elements with floating labels and sample data

I understand that it is done with focus but I can't get good results. I look forward to your responses, thank you.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Let suppose You have two inputs next to each other in the same dom tree level:

html:

<input  (change)="onInput1ChangeEvent($event)" />
<input #secondInput  />

ts: //get input tag reference

import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
@Component({
  selector: 'app-Component',
  templateUrl: './app-Component.html',
  styleUrls: ['./app-Component.scss'],
})
  @ViewChild('secondInput') secondInput!: ElementRef;
constructor(){}

  onInput1ChangeEvent(event: any){
let do = event.target.value;
//let suppose max length value to focus next input is 10 letters

    let maxVal=10;
    let doLength =0;
    if(!!do){
    doLength = do.length;
    let focusNextInput = doLength == maxVal
    if(focusNextInput){
    //focus next input
        this.secondInput.nativeElement.focus();
    }
    
    
    }
      }

After a long discussion with Mr Eliseo about the easiest and more clean code method to deal with html template directly and with less variables:

html:

<input #firstInput (input)="!!firstInput.value && firstInput.value.length==10 && secondInput.focus()">
<input #secondInput  />

And finally I have to say thank You to Mr Eliseo for improving this answer and guiding us to get the best method to fix this issue.

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