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 Angular Keypress Enter Event on Input Example

Angular Keypress Enter Event on Input Example ?

September 21, 2022March 16, 2024

Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Keypress Enter Event on Input with Example. In core javascript we use onkeypress event to handle the key events of input but in angular it there is different way to…

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 Laravel Multi Select Tag Autocomplete Using Select2

Laravel Multi Select Tag Autocomplete Using Select2

July 19, 2022July 19, 2022

In this article we will learn to use Laravel Multi Select Tag Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. In this article we will cover multiple…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version