How to Dynamically Add new Textbox using jQuery in JavaScript

How to Dynamically Add new Textbox using jQuery in JavaScript

Introduction

In this tutorial we will create a How to Dynamically Add new Textbox using jQuery in JavaScript. This tutorial purpose is to teach you how to add new textboxes dynamically. This will thoroughly make use basic functionality of jQuery . I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application that uses jQuery plugin to enhance the interact content of your website. I will give my best to provide you the easiest way of creating this program Dynamically add new textbox. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the html form and button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Dynamically Add new Textbox using jQuery</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-4">
  17. <form>
  18. <div class="form-group">
  19. <input type="text" id="input" class="form-control"/>
  20. </div>
  21. <button type="button" id="add" class="btn btn-primary">Add Input</button>
  22. <br /><br />
  23. <div id="input_container"></div>
  24. <button type="button" style="display:none;" id="submit" class="btn btn-success">Submit</button>
  25. </form>
  26. </div>
  27. </div>
  28. <script src="js/jquery-3.2.1.min.js"></script>
  29. <script src="script.js"></script>
  30. </body>
  31. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will add new textbox dynamically when the buttons is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $('#add').on('click',function(){
  3. var input = $('#input').val();
  4. if(input != ""){
  5. $('#submit').show();
  6. var newInput = $(
  7. '<div class="form-group">'
  8. +'<input type="text" placeholder="'+input+'" class="form-control newInput" required="required"/>'
  9. +'</div>'
  10. );
  11. newInput.appendTo('#input_container');
  12. $('#input').val('');
  13. }else{
  14. alert("Please enter something");
  15. }
  16. });
  17.  
  18. $('#submit').on('click', function(){
  19. var list=[];
  20. $('.newInput').each(function(){
  21. list.push($(this).val());
  22. });
  23.  
  24. alert(list.join(', '));
  25. });
  26. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. In this code we just simply bind the button to trigger the event for adding new textbox. The function includes some variables and a certain function that will add new textbox using the append function of jQuery.

Output:

The How to Dynamically Add new Textbox using jQuery in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Dynamically Add new Textbox using jQuery in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment