JavaScript - Generate Input Field Using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 107 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Generate Input Field Using jQuery</h3>
  16. <hr style="border-top:1px dotted;"/>
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-group">
  20. <input type="text" id="input" class="form-control"/>
  21. </div>
  22. <center><button type="button" id="add" class="btn btn-primary">Add Input</button></center>
  23. <br />
  24. <div id="input_container"></div>
  25. <center><button type="button" style="display:none;" id="submit" class="btn btn-success">Submit</button></center>
  26. </form>
  27. </div>
  28. </div>
  29. <script src="js/jquery-3.2.1.min.js"></script>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.