JavaScript - create link element dynamically

0 points
Created by:

In this article, we would like to show you how to create a link element dynamically using JavaScript.

Quick solution:

var linkElement = document.createElement('a');
link.href = '';
link.textContent = 'Example link text...';


Practical example

In this example, we create link (a) element using createElement() method. Then after specifying its attributes such as href and text, we append the link to the body element.

// ONLINE-RUNNER:browser;

<!doctype html>

    // create link dynamically
    var link = document.createElement('a');

    // set href attribute
    link.href = '';

    // set link text
    link.text = 'Example link text...';

    // append link element to the body



  1. Document.createElement() - Web APIs | MDN
  2. Node.appendChild() - Web APIs | MDN
  3. <a>: The Anchor element - HTML: HyperText Markup Language | MDN

Alternative titles

  1. JavaScript - create anchor element dynamically
  2. JavaScript - create a element dynamically (with href and text)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂