How to Create a Website Link Dynamically using jQuery

How to Create a Website Link Dynamically using jQuery


In this tutorial we will create a How to Create a Website Link Dynamically using jQuery. This tutorial purpose is to teach how to create your own website link dynamically. This will cover all the important parts for creating html form for generating link. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is easy to understand just follow my instruction I provided and you can do it without a problem. This program is useful if you want to put a website link for your website. I will try my best to give you the simplest way of creating this program How to Create a Website Link Dynamically. 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

Lastly, this is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html form for generating a url link. 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. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <nav class="navbar navbar-default">
  6. <div class="container-fluid">
  7. <a class="navbar-brand" href="">Sourcecodester</a>
  8. </div>
  9. </nav>
  10. <div class="col-md-3"></div>
  11. <div class="col-md-6 well">
  12. <h3 class="text-primary">How to Create a Website Link Dynamically</h3>
  13. <hr style="border-top:1px dotted #ccc;"/>
  14. <div class="col-md-6">
  15. <div class="form-group">
  16. <input type="text" placeholder="Website name" id="name" class="form-control"/>
  17. <input type="text" placeholder="URL" id="url" class="form-control"/>
  18. <button type="button" id="create" class="btn btn-success btn-block"><span class="glyphicon glyphicon-link"></span> Create Link</button>
  19. </div>
  20. </div>
  21. <div class="col-md-6">
  22. <h4>My Links</h4>
  23. <div id="result"></div>
  24. </div>
  25. </div>
  26. <script src="js/jquery-3.2.1.min.js"></script>
  27. <script src="script.js"></script>
  28. </body>
  29. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will store the inputted data and will display it as a website link. 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. $('#create').on('click', function(){
  3. var name = $('#name').val();
  4. var url = $('#url').val();
  6. if(name == "" || url == ""){
  7. alert("Required Field!");
  8. }else{
  9. $('#result').append("<a href='"+url+"' target='_blank'>"+name+"</a><br />");
  10. $('#name').val('');
  11. $('#url').val('');
  12. }
  13. });
  14. });

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. After that we bind the button to trigger a certain functions using the on() function. We then create a variable that will hold the html data. To display our website link we just simply append the inputted html form data back to the page using the jQuery append() function.


The How to Create a Website Link Dynamically using jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Website Link Dynamically 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment