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 ngFor and For loop over object or Array in angular

ngFor and For loop over object or Array in angular

March 30, 2022October 4, 2022

Loops are used to iterate through objects or array, In angular we can use ngFor and For loop over array or object in angular, ngFor is a template structural directive which is used to iterate a loop in template file whereas for and each is used to iterate the loop…

Read More

Show dropdown list from ajax response in jQuery

September 10, 2021September 10, 2021

Showing Dynamic Select Box using jQuery and Php,MySql In this tutorial, we are going to learn the dropdown list using AJAX. Loading dropdown values from AJAX requires knowledge of javascript, jQuery, php and html. Sometimes it requires to load the dropdown like countries, state, city list dependently. In this case…

Read More
Uncategorized setup simple jQuery form validation

How to setup simple jQuery form validation?

September 15, 2021September 29, 2021

In this tutorial, we are going to learn jQuery form validation using jQuery validation plugin. this plugin have many features like to validate text, email, phone number , sync Ajax request etc. Let’s begin with step by step guide: Step 1: Create a html file Firstly we are going to…

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