'how to create a text box when button is pressed in Js?
I want to create a button in JS and when you press it, it will create a text box above the button. But the number of textboxes is unknown. is it possible? Thank you
Solution 1:[1]
You can do it like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Ronak</title>
</head>
<body>
<div id="textboxContainer">
</div>
<button onclick="addTextbox(event)">Click Me!</button>
<script type="text/javascript">
function addTextbox(event) {
var input = document.createElement("input");
input.setAttribute("type", "text");
document.getElementById("textboxContainer").appendChild(input);
}
</script>
</body>
</html>
Solution 2:[2]
There are one way. You have to append it. With Jquery it is something like this:
<html>
<body>
<button></button>
<p id="new"></p>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
$("button").click(()=>{
$("p#new").append("Your input textbox<input ---/>")
})
</script>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ronak Shah |
| Solution 2 | Sohan Arafat |
