How to create a link in JavaScript ?

At whatever point you are fostering a web application or site you should manage URLs and connections which are often used to explore a client starting with one page then onto the next, as there could be no alternate method for exploring your clients without connections and URLs. Henceforth, you want to make them and put them in the specific spots where you need to explore the clients.

For what reason do you want to make joins with JavaScript

HTML permits you to put joins inside the anchor tag under the href characteristic. Nonetheless, while fostering a JavaScript-based application where you need to deal with everything automatically, you really want to make interfaces progressively and appoint them to the href property of the HTML anchor tag.

This is the significant motivation behind why you really want JavaScript to make a connection and this is the thing that we will discuss in this article so we should have a profound plunge at how we can undoubtedly make a connection utilizing JavaScript. I have a string for a title and a string for a connection. I don’t know how to assemble the two to make a connection on a page utilizing Javascript. Any assistance is valued.

Example 1: In this example, the node is created and the attributes are set by the JavaScript methods.

            How to create a link in JavaScript ?
    <body style = "text-align:center;">
        <h1 style = "color:green;"
        <p id = "GFG_UP" style =
            "font-size: 19px; font-weight: bold;">
        <button onclick = "GFG_Fun()">
            click here
        <p id = "GFG_DOWN" style =
            "color: green; font-size: 24px; font-weight: bold;">
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = "Click on the button to generate "
                    + "a link using JavaScript.";
            function GFG_Fun() {
                // Create anchor element.
                var a = document.createElement('a'); 
                // Create the text node for anchor element.
                var link = document.createTextNode("This is link");
                // Append the text node to anchor element.
                // Set the title.
                a.title = "This is Link"; 
                // Set the href property.
                a.href = "site"; 
                // Append the anchor element to the body.

Approach to Create a link

To create a link programmatically, we first understand what exactly we need to do.

First, we need to create an anchor tag using javaScript:


Create an anchor <a> tag
For creating an anchor, we can utilize the code provided below. Create an element(<a> tag) and assign it to the variable named “anchor” as we will need it later:

let anchor = document.createElement(‘a’);

After creating the anchor tag, we need to write some text inside the <a> tag as demonstrated below:

<a>Linuxhint website<a/>

Write text into the <a> tag
For writing some text inside the <a> tag, first create a text node and then append that text node as a child to the anchor tag. The code for writing text into the anchor tag will go like this:

//create a text node and assign it to the “link” variable.
let textNode = document.createTextNode(“Linuxhint Website”);

// Append the textNode as a child to anchor.

At this stage, the text is appended into the anchor <a> tag. Now, we need to put the link in the href attribute of the anchor <a> tag as shown below.

<a href=“”>Linuxhint Website<a/>

Set the href attribute of <a> tag
To put the link in the href attribute of <a> tag, the following line of javaScript code will be used:

anchor.href = “”;

After setting the href attribute, the only thing left is to append the <a> tag where we want it to be put.

Append the <a> tag to HTML body
To append the anchor tag to the body, use the following line of code.


Alright, you have learned all the procedure to create a link using javaScript. Let’s go through an example to demonstrate the results.


Let’s take a simple example where we will simply create a link and append it to the HTML body and will checkout the behavior of the link if it is working or not.

First, we will create a button and at the click of that button the createLink() method will be called.

<button onclick = “createLink()”>
click here

All the code for creating the link will be written inside the createLink() function and the whole JavaScript code will go like this:

function createLink() {
let anchor = document.createElement(‘a’);
let link = document.createTextNode(“Linuxhint Website”);
anchor.href = “”;

How do I create a link using javascript?

      var a = document.createElement('a');
      var linkText = document.createTextNode("my title text");
      a.title = "my title text";
      a.href = "";

With JavaScript

  1. var a = document.createElement('a');
    a.innerHTML = desiredText;
    // apend the anchor to the body
    // of course you can append it almost to any other dom element
  2. document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';

    or, as suggested by @travis :

    document.getElementsByTagName('body')[0].innerHTML +=;
  3. <script type="text/javascript">
    //note that this case can be used only inside the "body" element
    document.write('<a href="'+desiredLink+'">'+desiredText+'</a>');

With JQuery

  1. $('<a href="'+desiredLink+'">'+desiredText+'</a>').appendTo($('body'));
  2. $('body').append($('<a href="'+desiredLink+'">'+desiredText+'</a>'));
  3. var a = $('<a />');

In all the above examples you can append the anchor to any element, not just to the ‘body’, and desiredLink is a variable that holds the address that your anchor element points to, and desiredText is a variable that holds the text that will be displayed in the anchor element.

Create links using JavaScript:

<script language="javascript">
document.write("<a href=\"\">");
document.write("Your Title");


<script type="text/javascript">
document.write('Your Title'.link(''));


<script type="text/javascript">
newlink = document.createElement('a');
newlink.innerHTML = 'Google';
newlink.setAttribute('title', 'Google');
newlink.setAttribute('href', '');

Also Read: Round() in C++

Leave a Reply

Your email address will not be published. Required fields are marked *