Skip to main content

JavaScript - Generate Input Field Using jQuery


In this tutorial we will create a Generate Input Field using jQuery. This code will dynamically generate new input field when the user click the add button. The code itself use jQuery onclick function to generate new input field using appendTo() a jQuery built-in function that will append all the elements as a child of the parent element. This is a user-friendly program feel free to modify it.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">JavaScript - Generate Input Field 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. <center><button type="button" id="add" class="btn btn-primary">Add Input</button></center>
  22. <br />
  23. <div id="input_container"></div>
  24. <center><button type="button" style="display:none;" id="submit" class="btn btn-success">Submit</button></center>
  25. </form>
  26. </div>
  27. </div>
  28. <script src="js/jquery-3.2.1.min.js"></script>
  29. <script src="js/script.js"></script>
  30. </body>
  31. </html>

Creating the Script

This code contains the script of the application. This code will generate an input field when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  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. });

There you have it we successfully created a Generate Input Field using jQuery. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.