How to Add an Input Field on Button Click in JavaScript ? Last Updated : 21 Mar, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript allows us to dynamically add an input field upon button click. We can use the below approaches for adding an input field on Button Click in JavaScript. Table of Content Using DOM ManipulationUsing Template Literal and InnerHTMLUsing DOM ManipulationDOM manipulation is used to dynamically create and append input fields upon clicking the "Add Input Field" button. We create a div wrapper for each input field to ensure the proper layout and styling. Example: The below example uses DOM Manipulation to Add an input Field on a Button Click in JavaScript. HTML <!DOCTYPE html> <head> <title>Example 1</title> <style> body { font-family: Arial, sans-serif; text-align: center; } .container { display: block; text-align: center; margin-top: 20px; } .input-field { width: 200px; padding: 8px; border: 1px solid #ccc; border-radius: 5px; margin: 10px auto; } .add-button { padding: 8px 20px; background-color: green; color: #fff; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <h3>Using DOM Manipulation</h3> <div class="container"> <button class="add-button" onclick="addFn()">Add Input Field</button> <div id="inputFields"></div> </div> <script> function addFn() { const divEle = document.getElementById("inputFields"); const wrapper = document.createElement("div"); const iFeild = document.createElement("input"); iFeild.setAttribute("type", "text"); iFeild.setAttribute("placeholder", "Enter value"); iFeild.classList.add("input-field"); wrapper.appendChild(iFeild); divEle.appendChild(wrapper); } </script> </body> </html> Output: Using Template Literal and InnerHTMLIn this approach, we are using template literals and innerHTML to dynamically generate HTML content upon clicking the "Add Input Fields" button. Two input fields are added inside a div wrapper each time the button is clicked. Example: The below example uses Template Literal and InnerHTML to Add an input Field on a Button Click in JavaScript. HTML <!DOCTYPE html> <head> <title>Example 2</title> <style> body { font-family: Arial, sans-serif; text-align: center; } .container { display: block; text-align: center; margin-top: 20px; } .input-field { width: 200px; padding: 8px; border: 1px solid #ccc; border-radius: 5px; margin: 10px auto; } .add-button { padding: 8px 20px; background-color: green; color: #fff; border: none; border-radius: 5px; cursor: pointer; } </style> </head> <body> <h3>Using Template Literal and InnerHTML</h3> <div class="container"> <button class="add-button" onclick="addFn()">Add Input Fields</button> <div id="inputFields"> </div> </div> <script> function addFn() { const divEle = document.getElementById("inputFields"); divEle.innerHTML += ` <div> <input type="text" placeholder="Enter value" class="input-field"> <input type="text" placeholder="Enter value" class="input-field"> </div> `; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Add an Input Field on Button Click in JavaScript ? G gauravggeeksforgeeks Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Add Button Inside an Input Field in HTML ? Integrating a button inside an input box is a common design pattern in web development, often seen in search bars, where a âsearchâ button sits within the text input field, or in password fields that include a âshow/hideâ toggle. This UI element enhances user experience by offering a clear, interact 3 min read How to add input fields dynamically on button click in AngularJS ? The task is to add an input field on the page when the user clicks on the button using AngularJs. Steps: 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 fi 2 min read How to add readonly attribute to an input tag in JavaScript ? Use the setAttribute() Method to add the read-only attribute to the form input field using JavaScript. setAttribute() Method This method adds the defined attribute to an element and gives it the defined value. If the specified attribute is already present, then the value is being set or changed. Syn 2 min read How to Add an ID to Element in JavaScript ? In JavaScript, the ID is the unique identifier through which the elements can be identified and manipulated if required. We can add an ID to an element in JavaScript using various approaches that are explored in this article with practical demonstration. Below are the possible approaches: Table of C 2 min read How to Add event listener to Button in JavaScript ? In web development adding an event listener to a button is a common task that allows us to execute JavaScript code in response to user interactions, such as clicks. This helps us to create interactive and flexible web pages. Using addEventListener()The addEventListener method is a versatile and wide 1 min read JavaScript - How to Add, Edit and Delete Data in HTML Table? To add edit and delete features in an HTML table with JavaScript, create buttons for each action. Use JavaScript functions to add new rows, edit existing data, and remove rows as needed.Approach to Add Edit and Delete DataThe addData() method obtains the data from the input fields and creates a fres 3 min read Set the Value of an Input Field in JavaScript We will learn how you can set the value of an input field in JavaScript. We will set user input to the placeholder value of the input field using JavaScript.Below are the approaches to set the value of an input field in JavaScript:Table of ContentBy using the innerHTML propertyBy using setAttribute 2 min read How to Simulate a Click in JavaScript? Simulating a click in JavaScript means triggering a click event on an HTML element using code, rather than requiring manual user interaction. This technique is useful for automating actions like clicking buttons, links, or other interactive elements, enhancing web functionality, and triggering hidde 3 min read How to Add Input Field inside Table Cell in HTML ? In HTML, adding input fields inside the table cells can be done to make the application user interactive and useful in the scenarios of building forms, data entry systems, etc. Below are the approaches to add input field inside a Table Cell in HTML: Table of Content Using 'input' tagUsing data Attri 3 min read How to count the number of times a button is clicked using JavaScript ? At times, it becomes necessary to monitor the number of times a user clicks a button. In this article, we are going to learn how to count the number of times a button is clicked using JavaScript Below are the approaches to count the number of times a button is clicked using JavaScript Table of Conte 3 min read Like