Categories
JavaScript Answers

How to add a text link inside a div using JavaScript?

Spread the love

Sometimes, we want to add a text link inside a div using JavaScript.

In this article, we’ll look at how to add a text link inside a div using JavaScript.

How to add a text link inside a div using JavaScript?

To add a text link inside a div using JavaScript, we can set the innerHTML property of the div to a string with the link code.

For instance, we write:

<div>

</div>

to add a div.

Then we write:

const div = document.querySelector('div')
div.innerHTML = '<a href="http://www.example.com">example</a>'

to select the div with document.querySelector.

And then we set its innerHTML property to the link string.

Now we should see the link displayed on screen.

Conclusion

To add a text link inside a div using JavaScript, we can set the innerHTML property of the div to a string with the link code.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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