How to add a list of hyperlinks (with their events and properties) dynamically to a div in Javascript?
As long as your anchor has an ID you can link to a location in your view by clicking the div you will just need some javascript. Setting the window. location. hash value will put the focus of the view on the selected ID.
Using JavaScript In vanilla JavaScript, you can use the document. createElement() method, which programmatically creates the specified HTML element. The following example creates a new anchor tag with desired attributes and appends it to a div element using the Node. appendChild() method.
With document. createElement() method you can create a specified HTML element dynamically in JavaScript. After creation you can add attributes. If you want the element to show up in your document, you have to insert in into the DOM-tree of the document.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With