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 How to create global variable in angular ?

How to create global variables in angular ?

October 3, 2022March 16, 2024

In this article, we will learn about the create global variables in Angular and how to use them. We will also see how to use them across the application by importing the file. There are different ways to create global variables in angular. One way is to use the global…

Read More
Javascript Directives in angular

What is directives and types of directives in angular?

September 1, 2021February 8, 2024

Directives in angular are custom tags and custom attributes of html which is controlled by class and decorators. Using directives we can change the behaviour, style and logic of a specific area. Example: helloworld.component.ts Xyz.compenent.html <hello-world></hello-world> Output:Hello world component/directive Types of Directives in Angular: 1. Component : Angular page is…

Read More
Javascript How to check user agent in angular

How to check user agent in angular ?

October 18, 2022March 16, 2024

In this post, we will show you how to check user agent in Angular. user-agent header is a string that contains information about the browser and operating system. It is set by the browser and sent to the server with every request. It can also be used to determine if…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version