In this article I will show you how to add textbox dynamically using jQuery. The JQuery append() allows us to append the content into div element and remove a div using jQuery remove() method.
jQuery add remove texbox example code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Add/Remove textbox dynamically using jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnAdd").on("click", function () {
$("#textboxDiv").append("<div><br><inputtype='text'/><br></div>");
});
$("#btnRemove").on("click", function () {
$("#textboxDiv").children().first().remove();
});
});
</script>
</head>
<body style="border:1px solid #e8dbdb;width:450px;height:330px">
<h2>Add/Remove textbox dynamically using jquery</h2>
<div id="textboxDiv"></div>
<br />
<input type="button" id="btnAdd" value="Add" />
<input type="button" id="btnRemove" value="Remove" />
</body>
</html>
Description: Initially, I created a div element named textboxDiv , when you click on the add button, the div element with textbox is appended to the ID textboxDiv.
When you click the Remove button it will get removed by selecting the div with id textboxDiv and remove the last element form its child.
Output:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article