Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Set focus on input angular example

Set focus on input angular example

Aman Jain, September 25, 2022March 16, 2024

Angular is a great framework for building front-end web applications. One of its many features is the ability to set focus on input angular element. This can be useful in a variety of situations, such as when you want to focus on a particular input field after a user clicks a button.

There are a few different ways to set focus on an input angular element. One way is to using the autofocus attribute. This is a good option if you only want to set focus on an input element once, such as when a page is first loaded. Another way is to use the nativeElement.focus() method. This is a good option if you want to set focus on an input element dynamically, such as after a user clicks a button. Finally, you can use the @ViewChild decorator to inject a reference to the desired input element into your component class.

All methods will work in all angular version including angular 2, angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application.

Also Read: Angular Focusout Event on Input Example
Which method you use in Angular will depend on your particular use case. In most cases, either the autofocus attribute or the nativeElement.focus() method will be sufficient. However, if you need to set focus on an input element dynamically, the @ViewChild decorator is the best option.

Let’s learn the all methods

Method 1 : Set Auto focus Using AotoFocus Attribute in Angular

In this method we will use AutoFocus Attribute attribute to set the focus on input element in angular as follow

<input type="text" autofocus /> 

Here we used autofocus attribute to set the focus on page load in angular.

Method 2 : Set Focus using ViewChild in Angular

In this method we will use ViewChild to set the focus on input element in angular as follow

<input type="text" autofocus #search /> 
<button (click)='search.focus()'>Add Focus</button>

Here we used #search and on click of button we used search.focus to set the focus to input. By using this method we can set focus dynamically.

And if you want to set it from component

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  @ViewChild("search") search : ElementRef;
  constructor() {
  }
  focusIn(event:any) {
     this.search.nativeElement.focus();

  }
  ngOnInit() {}
}

Related

Javascript Angular angularautofocusfocusinput

Post navigation

Previous post
Next post

Related Posts

Javascript localStorage.setItem(key,value)

How to store data in local storage in angular ?

March 28, 2022

While working in angular application we want to store data in local storage for example want to store user login data or some calculations. In this article i will show you to store objects, array, integer, string etc. in local storage. We will going to use native API for storing…

Read More
Javascript How to show html string in angular template

How to show html string in angular template ?

March 31, 2022March 31, 2022

Angular built-in supports vulnerabilities and attacks in a web application like cross site scripting, however sometimes in our application we want to render the html string through bypassing the security of angular. To bypass the security, angular itself provides some methods and directives by which we can render or show…

Read More
Javascript Min Length and Max Length Validation in Angular 12 13

Min Length and Max Length Validation in Angular 12 / 13 ?

March 21, 2022March 21, 2022

Angular provides built-in library for validation, in the same way it gives validation methods to validate the string and numbers length as well. so in this tutorial i am going to explain how to use min length and max length validation in angular. In angular validation library it provides different…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version