The Wayback Machine - https://web.archive.org/web/20240626174811/https://www.geeksforgeeks.org/how-to-add-and-remove-input-fields-dynamically-using-jquery-with-bootstrap/
Open In App

How to add and remove input fields dynamically using jQuery with Bootstrap ?

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
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 button
  • Append() 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=
    <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.



Similar Reads

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 fields are there and button is created. There is a (
2 min read
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 approaches: Table of Content Using jQuery append()Using
3 min read
How to Dynamically Add/Remove Table Rows using jQuery ?
In this article, we will learn how to dynamically add/remove rows from an HTML table using jQuery. Before reading this article, make sure you have some basic idea about jQuery, its in-built methods, and element selection. jQuery provides us with a lot of methods to perform various tasks. To dynamically add and remove the rows from an HTML table, we
3 min read
How to fill all input fields automatically from database by entering input in one textbox using PHP ?
XAMPP is a free and open-source cross-platform web server solution developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreter for scripts written in the PHP programming language. The XAMPP server helps to start Apache and MySQL and connect them with the PHP file. Approach: We need to create a MySQL dat
4 min read
How to Add Background Color in Input and Text Fields using CSS ?
In this article, we will explore different approaches to adding background color to input and text fields using CSS. Customizing the appearance of input and text fields is an essential aspect of web design, and changing the background color is a powerful way to enhance the visual appeal of these elements. Method 1: Basic CSS StylingThe basic method
2 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 jQuery CDN from script tag to use jQuery on the p
2 min read
How to add CSS properties to an element dynamically using jQuery ?
In this article, we will see how to add some CSS properties dynamically using jQuery. To add CSS properties dynamically, we use css() method. The css() method is used to change the style property of the selected element. The css() method can be used in different ways. This method can also be used to check the present value of the property for the s
1 min read
How to dynamically add or remove items from a list in Vue.js ?
Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. Using Vue.js one can easily add or re
1 min read
How to Add href to React-Bootstrap-Table Column Dynamically ?
In this article, we are going to learn how to add a href to the react-bootstrap table column dynamically. In this article, we will create a React Bootstrap table and an input form to add data dynamically with a href. Prerequisites: React JSReact-BootstrapSteps to Create React Application And Installing Modules:Step 1: Create your react-app by using
4 min read
How to Add an Array Dynamically to React-Bootstrap Table ?
In this article, We are going to learn how can we dynamically add an array to a react-bootstrap table. While developing the ReactJS applications, we need to do various dynamic things like entering the data in real time. Also, inserting the array data in one go. In react-bootstrap, there is a Table component that is used to display the information i
6 min read
three90RightbarBannerImg