How to add anchor tags dynamically to a div in Javascript?
Asked Answered
C

7

51

How to add a list of hyperlinks (with their events and properties) dynamically to a div in Javascript?

Caucasus answered 1/4, 2011 at 23:0 Comment(0)
G
102

here's a pure Javascript alternative:

var mydiv = document.getElementById("myDiv");
var aTag = document.createElement('a');
aTag.setAttribute('href',"yourlink.htm");
aTag.innerText = "link text";
mydiv.appendChild(aTag);
Gans answered 1/4, 2011 at 23:8 Comment(7)
I'd use the innerText attribute instead of innerHTML, as it's safer when it comes to XSS. Not that there's a problem right here, but if OP is going to load link texts dynamically it's better to be on the safe side.Herzegovina
I don't know what goes wrong with me when I do that, but it doesn't work and no items are added to the div! Any idea?Caucasus
sorry i edited it. i switched mydiv and aTag in appendChild. try it again. here's a demo: jsfiddle.net/ducondez/mbTnHGans
want to add a hyperlink on the ribbon. I was able to add the hyperlink to the existing div. The Hyperlinks are getting increased by one for every action i do on the page. How i can make it(hyperlink) restrict to one?Waist
"backward update": in this accepted answer from 2010 https://mcmap.net/q/354697/-why-is-innertext-not-working-in-firefox-duplicate it is said that innerText is "the 'old Internet Explorer' way of doing it" and that textContent should be used instead. I added this because on Firefox innerText seems not to be working to me..Moreta
myDiv being the element's name?Snowblink
instead of inner html it's better to create a text node and append it, nevertheless up-votedLibretto
H
13

I recommend that you use jQuery for this, as it makes the process much easier. Here are some examples using jQuery:

$("div#id").append('<a href="' + url + '">' + text + '</a>');

If you need a list though, as in a <ul>, you can do this:

$("div#id").append('<ul>');
var ul = $("div#id > ul");

ul.append('<li><a href="' + url + '">' + text + '</a></li>');
Histogram answered 1/4, 2011 at 23:2 Comment(3)
Jquery uses javascript.. I agree that using Jquery is much easier to read here.Bobo
Jquery is not java script!Lorenzo
This doesn't answer the question.Trichome
A
2
var newA = document.createElement('a');
newA.setAttribute('href',"http://localhost");
newA.innerHTML = "link text";
document.appendChild(newA);
Ascensive answered 1/4, 2011 at 23:5 Comment(0)
G
1
<script type="text/javascript" language="javascript">
function createDiv()
{
  var divTag = document.createElement("div");            
  divTag.innerHTML = "Div tag created using Javascript DOM dynamically";        
  document.body.appendChild(divTag);
}
</script>
Gowen answered 1/4, 2011 at 23:6 Comment(0)
C
0

With jquery

  $("div#id").append('<a href=#>Your LINK TITLE</a>')

With javascript

   var new_a = document.createElement('a');
   new_a.setAttribute("href", "link url here");
   new_a.innerHTML = "your link text";
   //add new link to the DOM
   document.appendChild(new_a);
Corabelle answered 13/2, 2021 at 18:11 Comment(0)
S
0
var a = document.createElement('a');
a.target = '_blank';
a.href = '/solution_code/ + solution_code.id'
a.innerText = "Soution Code";
var container = document.getElementById('solution-code');
container.appendChild(a);
container.appendChild(document.createElement('br'));
Stung answered 3/5, 2021 at 6:36 Comment(0)
T
-1

One more variation wrapped up nicely where setAttribute isn't needed.

There are 3 lines that wouldn't be needed if Wetfox could dry off.

var saveAs = function (filename, content) {
    if(filename === undefined) filename = "Unknown.txt";
    if(content === undefined) content = "Empty?!";
    let link = document.createElement('a');
    link.style.display = "none"; // because Firefox sux
    document.body.appendChild(link); // because Firefox sux
    link.href = "data:application/octet-stream," + encodeURIComponent(content);
    link.download = filename;
    link.click();
    document.body.removeChild(link); // because Firefox sux
};

Thanks for the help.

Testee answered 28/1, 2017 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.