How to add and remove input fields dynamically using jQuery with Bootstrap ? Last Updated : 31 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to add and remove input fields dynamically using jQuery with Bootstrap. The input fields will be added and removed from the HTML document dynamically using jQuery.In this process, we will use the following multiple functions.Click() Method: Used to attach the click event to the buttonAppend() Method: Used to append the HTML of the new input field to the document.Remove() Method: Used to remove the input field from the document.Parent() Method: Used to select the parent element of a child element.CDN Links:<!-- Bootstrap CSS CDN --><link rel=”stylesheet” href=”//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css”><!-- Bootstrap JS CDN --><link rel=”stylesheet” href=”https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css”><!-- jQuery CDN --><script src=”//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js”></script>Approach: Firstly, add the jQuery, and Bootstrap CDN to the script or download them to your local machine.Create an input group and add an input field along with a button. i.e. this button will be used to delete this input field.Create a button and on clicking this button we will dynamically add the input fields.Now write the click() function to handle the adding and removing functionality.Use the append() method to add the input field code to the existing HTML document.$('#newinput').append(newRowAdd);Use the remove() method to remove the input field from the document.$(this).parents("#row").remove(); Add some styling to the page using the bootstrap.Example: In this example, the above methods are implemented to add and remove the input field. HTML <!doctype html> <html lang="en"> <head> <title>Add or Remove Input Fields Dynamically</title> <link rel="stylesheet" href= "//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <link rel="stylesheet" href= "https://cdn.jsdelivr.net/npm/bootstrap-icons@1.3.0/font/bootstrap-icons.css"> <script src= "//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script> <style> body { display: flex; flex-direction: column; margin-top: 1%; justify-content: center; align-items: center; } #rowAdder { margin-left: 17px; } </style> </head> <body> <h2 style="color:green">GeeksforGeeks</h2> <strong> Adding and Deleting Input fields Dynamically </strong> <div style="width:40%;"> <form> <div class=""> <div class="col-lg-12"> <div id="row"> <div class="input-group m-3"> <div class="input-group-prepend"> <button class="btn btn-danger" id="DeleteRow" type="button"> <i class="bi bi-trash"></i> Delete </button> </div> <input type="text" class="form-control m-input"> </div> </div> <div id="newinput"></div> <button id="rowAdder" type="button" class="btn btn-dark"> <span class="bi bi-plus-square-dotted"> </span> ADD </button> </div> </div> </form> </div> <script type="text/javascript"> $("#rowAdder").click(function () { newRowAdd = '<div id="row"> <div class="input-group m-3">' + '<div class="input-group-prepend">' + '<button class="btn btn-danger" id="DeleteRow" type="button">' + '<i class="bi bi-trash"></i> Delete</button> </div>' + '<input type="text" class="form-control m-input"> </div> </div>'; $('#newinput').append(newRowAdd); }); $("body").on("click", "#DeleteRow", function () { $(this).parents("#row").remove(); }) </script> </body> </html> Output:HTML is the foundation of web pages and is used for webpage development by structuring websites and web apps. You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.CSS is the foundation of web pages and is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples. Comment More infoAdvertise with us Next Article How to add and remove input fields dynamically using jQuery with Bootstrap ? P pulamolusaimohan Follow Improve Article Tags : HTML Geeks-Premier-League-2022 HTML-Questions jQuery-Questions Bootstrap-Questions Similar Reads How to Dynamically Add Select Fields Using jQuery? Select Fields can be added dynamically using jQuery. This increases the flexibility and interactivity of web forms, allowing users to add or remove options as needed. In this article, we will learn two different approaches to Dynamically Add Select Fields Using jQuery. These are the following approa 3 min read How to disable browser autofill on input fields using jQuery ? In this article, we will see how to disable the browser auto fill property on the input field. For that, HTML page is created in which jQuery CDN is imported to use jQuery and its code is written. Approach: Create a basic HTML page having at least one input field in it with the "id" attribute.Import 2 min read How to Add and Remove HTML Attributes with jQuery? Using jQuery, we can dynamically add and remove HTML attributes such as placeholder, style, and more to enhance the interactivity and functionality of web elements based on user actions. We will explore two different approaches to adding and removing HTML attributes with jQuery. Below are the possib 2 min read How to Dynamically Add/Remove Table Rows using jQuery? We will dynamically add/remove rows from an HTML table using jQuery. jQuery provides us with a lot of methods to perform various tasks. To dynamically add and remove the rows from an HTML table, we are also going to use some of these jQuery methods like append(), remove(), etc.Adding a rowTo add a r 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 create a Week Input using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Week Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr 1 min read How to remove all attributes of an HTML element using jQuery ? In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, f 1 min read How to find all input elements that are enabled using jQuery? The jQuery " :enabled" pseudo-class selector helps to find all the input element that are enabled. This pseudo-class selector should only be used for selecting HTML elements that support the disabled attribute i.e (<button>, <input>, <textarea>, <select>, <option>, < 2 min read Create a Form Dynamically using Dform and jQuery If you have ever used Google forms, you might have wondered how does it work. How an end-user can create their form dynamically. If these questions ever came across your mind, then this article can help you. Prerequisite: Basic of HTML and CSSBasic of JQuery The plugin which we are going to use here 3 min read How to show/hide div element depending multiple values using Bootstrap and jQuery ? Bootstrap and jQuery have been used together to develop interactive web applications. Both Bootstrap and jQuery are free and open source which makes it popular among developers. This article deals with showing and hiding division depending on user input with the help of Bootstrap and jQuery. Below s 5 min read Like