How to add a list of hyperlinks (with their events and properties) dynamically to a div in Javascript?
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);
I'd use the
innerText
attribute instead ofinnerHTML
, 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.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?
sorry i edited it. i switched mydiv and aTag in appendChild. try it again. here's a demo: jsfiddle.net/ducondez/mbTnH
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?
"backward update": in this accepted answer from 2010 stackoverflow.com/a/22990890/988591 it is said that
innerText
is "the 'old Internet Explorer' way of doing it" and thattextContent
should be used instead. I added this because on FirefoxinnerText
seems not to be working to me..