A simple HTML and jQuery solution to dynamically add and remove input fields in a form. This functionality is useful for forms requiring variable amounts of data input, such as name-value pairs.
- Dynamically add new input rows.
- Remove specific input rows.
- Real-time updates to the form layout.
Create a basic HTML form with the following elements:
- An initial input section (
#rowsection
) for enteringName
andValue
. - A button (
#addrow
) to dynamically add new input rows. - A container (
#newinputsection
) where dynamically added rows will be appended.
The #addrow button listens for a click event.
- On click, it generates a new block of HTML (newRowAdd) containing:
- A (
Name
) input field. - A (
Value
) input field. - A Remove button to delete the row.
- The generated HTML is appended to the #newinputsection container.
- Using (
$("body").on()
), a click event listener is attached to dynamically added #removeRow buttons. - On click, the (
parents("#rowsection")
) is targeted and removed, deleting the associated input group.
- Html
- CSS
- jQuery
HTML Example:
<div id="rowsection">
<input type="text" name="name[]" placeholder="Enter Name">
<input type="text" name="value[]" placeholder="Enter Value">
<button id="addrow">Add Row</button>
</div>
<div id="newinputsection"></div>