How to Create an Anchor Link in JavaScript?

Estimated read time 1 min read

To create an anchor link in JavaScript, you can use the document.createElement() method to create an <a> element and set its href attribute to the desired URL. Here’s an example code snippet that demonstrates how to create an anchor link in JavaScript:

const link = document.createElement("a");
link.href = "";
link.textContent = "Click me!";

In this code, a new a element is created using the document.createElement() method.

The href attribute of the a element is set to the desired URL.

The textContent property of the a element is set to the text that you want to display in the link, such as “Click me!”.

Finally, the a element is appended to the body of the document using the appendChild() method.

When this code is executed, it creates an anchor link with the specified URL and text content and appends it to the body of the document. You can modify this code to create an anchor link with your desired URL and text content.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply