To add "href" attribute to a link dynamically using JavaScript, we call the setAttribute
method.
For instance, we write
const a = document.getElementById("link");
a.setAttribute("href", url);
to select the link with getElementById
.
Then we call setAttribute
to set the href attribute value to url
.