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
- Your system's memory configuration has changed since it entered hibernation.
- Save image to client browser using jQuery?
- Get filename of image jQuery?
- How to get the image src using JavaScript?
- System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Net.Http.Formatting' with identity 'System.Net.Http.Formatting, Version=4.0.0.0
- Cordova Android build error "Cannot read property 'length' of undefined". An error occurred while running subprocess cordova.
- ERROR in The Angular Compiler requires TypeScript >=3.9.2 and <4.0.0 but 3.8.3 was found instead
- Code ENOLOCAL Could not install from "android" as it does not contain a package.json file.
Related Article