Open In App
Related Articles

How to add input fields dynamically on button click in AngularJS ?

Improve Article
Save Article
Like Article

The task is to add an input field on the page when the user clicks on the button using AngularJs.


  • The required component for the operation is created (add-inputComponent).
  • In that component, html file (add-input.component.html) required html is written.
  • In that HTML, the main div for input fields are there and button is created.
  • There is a (click) event on ADD button that redirects on the working user defined function in add-input.component.ts.
  • In the user defined function, the createElement() method is used to create a division every time the user clicks on the button and innerHTML() property is used to make an input field.
  • Then the created element div is append to the main div of add-input.component.html by appendChild() method.

Below is the implementation of above steps:

// Necessary imports of component is done
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AddInputComponent } from './add-input/add-input.component';
  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }


<!-- Main div and ADD button is created -->
  <div class="showInputField"></div>
  <!-- The add() function is called -->
  <button (click)="add()">ADD</button>


// The class function add() is made that contains
// the necessary code for the action
import { Component, OnInit } from '@angular/core';
  selector: 'app-add-input',
  templateUrl: './add-input.component.html',
  styleUrls: ['./add-input.component.css']
export class AddInputComponent implements OnInit {
  constructor() { }
  ngOnInit(): void {
    let row = document.createElement('div');  
      row.className = 'row';
      row.innerHTML = `
      <input type="text">`;


  • Before Clicking the Button:
  • After Clicking the Button:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 May, 2020
Like Article
Save Article
Similar Reads
Related Tutorials