Showing posts with label Add More Input Fields. Show all posts

jQuery Add/Remove Input Fields Dynamically with Limit

jqyer-add-remove-input-fields-part2
Adding or removing multiple input fields with a limit set. If you have added a certain limit of input fields you will get an alert or something. But before that, make sure that you have read the previous part of this tutorial jQuery Add/Remove Input Fields Dynamically

jQuery

So far, We have the below jQuery set up from previous tutorial.
We can limit adding additional fields like below:

Limitation

We are checking the length of the divs added inside of the #items div
If it is less than or equal to 5, then we can append another div otherwise we can show an alert.
So totally we can add only 5 additional input fields.
You can set your own limit.
That’s it for the tutorial

Add More Input Fields Using jQuery With Remove Button


jQuery Add/Remove Input Fields Dynamically
By using jquery you can add or duplicate more input fields with delete button near to it and you can remove the filed on delete button click.

HTML Markup

First we have a single input box inside of a div with the id of “items” and a button which says “Add Field”.
Inside of $(document).ready function place the below jQuery code.

jQuery

Now, whenever you click on “Add Field” a new input field with delete button will be added.
When a delete button is clicked we need to remove the corresponding input field using the below code.

Delete button

That’s it for this tutorial. In next tutorial we will learn how to limit adding input fields.