Given an HTML document and the task is to create a JavaScript link and add it to the document using JavaScript.
Approach:
- Create an anchor <a> element.
- Create a text node with some text which will display as a link.
- Append the text node to the anchor <a> element.
- Set the title and href property of the <a> element.
- Append <a> element in the body.
Example 1: In this example, the node is created and the attributes are set by the JavaScript methods.
<!DOCTYPE HTML> <html> <head> <title> How to create a link in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> 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. a.appendChild(link); // Set the title. a.title = "This is Link"; // Set the href property. a.href = "https://www.geeksforgeeks.org"; // Append the anchor element to the body. document.body.appendChild(a); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: This example is similar to the above but uses prepend() method to add anchor element to the body.
<!DOCTYPE HTML> <html> <head> <title> How to create a link in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <button onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> 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. a.appendChild(link); // Set the title. a.title = "This is Link"; // Set the href property. a.href = "https://www.geeksforgeeks.org"; // Append the anchor element to the body. document.body.prepend(a); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to create an HTML button that acts like a link?
- How to Create Link Tooltip Using CSS3 and jQuery ?
- How to create a nofollow link using HTML5 ?
- Open a link without clicking on it using JavaScript
- How to display confirmation dialog when clicking an <a> link using JavaScript / jQuery ?
- How to replace plain URL with link using JavaScript ?
- How to set smooth scroll after clicking the link using JavaScript ?
- How to remove underline from link using JavaScript ?
- Node.js | fs.link() Method
- PHP | link( ) Function
- How to disable a link using only CSS?
- CSS | :link Selector
- HTTP headers | Link
- HTML | DOM Link Object
- HTML | DOM Link disabled Property
- HTML | DOM Link type Property
- HTML | DOM Link sizes Property
- HTML | DOM Link hreflang Property
- HTML | DOM Link href Property
- HTML | DOM Link rel Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


