Home > Software engineering >  how to create a text box when button is pressed in Js?
how to create a text box when button is pressed in Js?

Time:02-04

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

CodePudding user response:

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>

CodePudding user response:

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>
  •  Tags:  
  • Related