I’m new to HTML and CSS and I hope to get some help here :D.
I want to create an <a>
element at a certain div container with the id navigationPaths, which would be this div container.
JavaScript
x
2
1
<div id="navigationPaths">
2
This is the part of my javascript code which I want to append to this ID. This is what i already found.
JavaScript
1
7
1
var a = document.createElement('a');
2
var linkText = document.createTextNode("Path");
3
a.appendChild(linkText);
4
a.title = "Path";
5
a.href = "http://google.com";
6
document.body.appendChild(a);
7
The functions I found doesn’t really suit my problem so I hope that someone here knows how this works.
Advertisement
Answer
As a postscript to Phil’s comment, if you didn’t want to faff around creating DOM elements, you could create some fully-formed HTML by inserting an HTML string into the element.
JavaScript
1
3
1
const anchor = '<a title="path" href="http://google.com">Path</a>';
2
const el = document.querySelector('#navigationPaths');
3
el.insertAdjacentHTML('beforeend', anchor);
JavaScript
1
1
1
<div id="navigationPaths">New anchor: </div>