Angular Directive To Fix Autofocus On Dynamically Generated Controls

ngx-autofocus-fix is ​​an Angular 5+ directive to fix autofocus on dynamically generated controls. Uses [Autofocus] selector, no need to change your HTML template.

autofocus attribute angular, dynamically set focus javascript, set autofocus in javascript, input autofocus not working, autofocus input angular, ngx autofocus fix

Advantages over other libraries

  • Uses native HTML property autofocus as delimiter!
  • No custom delimiters, no need to change your HTML template.
  • Works with the original DOM. It doesn’t use any dependencies (jQuery, Lodash, etc.).
  • Can use input attributes or provide global options via AutofocusFixConfig
  • 100% coverage, over 60 test units.
  • E2E tests for 8,7,6 and 5 versions of Angular including the e2e test for Angular Material Input.
  • The library understands an extensive list of input data. (null / NaN / ‘true’ / [] / …).
  • Supports asynchronous focus (optionally wrapping. focus() executed with setTimeout()).
  • Works perfectly with angular materials. (There is an E2E test)
  • It works with AOT mode.

How to make use of it:


# Yarn
$ yarn add ngx-autofocus-fix

$ npm install ngx-autofocus-fix --save

Import the library in your Angular app, for example in the AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AutofocusFixModule } from 'ngx-autofocus-fix'; // <--- new code

import { AppComponent } from './app.component';

  declarations: [
  imports: [

    AutofocusFixModule.forRoot(), // <--- new code
  providers: [],
  bootstrap: [ AppComponent ]
export class AppModule { }

You can now use the autofocus directive in app.component.html

<input autofocus
       placeholder="I have autofocus"
<button (click)="showInput = !showInput">Toggle Input</button>

Fix Autofocus On Dynamically Created Controls, ngx-autofocus-fix Plugin/Github, autofocus button angular

See Demo And Download

Official Website(korniychuk): Click Here

This superior jQuery/javascript plugin is developed by korniychuk. For extra advanced usage, please go to the official website.

Related Posts


Create Side Sliders Input Values Using Mouse Drag | Pointer Lock

HTML Range Slider is a lightweight library to create side sliders to adjust values easily and precisely by making use of the Pointer Lock API. Side Slider…


Smooth and Lightweight Parallax Scroll Library in Pure Javascript

Lightweight and seamless parallax scrolling library implemented in pure javascript using hardware acceleration for additional performance. Main Features Extremely lightweight with no dependencies A few kilobytes of pure…


How to Convert Form Data to JSON with HTML Forms | FormsJS

FormsJS is a simple-to-use JavaScript library that covers type subject values to JSON in real time. The items containing the data category will be analyzed automatically. It…


A Small jQuery Extension to Convert An Editable HTML Table

Editable Table is a small jQuery extension to convert an editable HTML table for fast data entry and validation. A small jQuery extension to convert a static…

Simple jQuery Plugin for Embedding YouTube Videos As Cover Background is a jQuery plugin built to facilitate YouTube embeds as cover wallpaper using the YouTube Embed API. There is another jQuery Youtube Video Background plugin that…


Interactive Data Table Generator with JS/jQuery and JSON | Tabulator

Tabulator allows you to create interactive tables in seconds from any HTML Table, JavaScript array, AJAX data source, or JSON format data. Just include the library in your…