<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div>
<p id="test"></p>
</div>
<script>
var newLink = document.createElement('a'); // #1
newLink.href = 'http://google.fr'; // #2
document.getElementById('test').appendChild(newLink); // #3
</script>
</body>
</html>
This is not working, and I can’t understand why.
- I create a new element
<a> - I add the href to this element (
<a href="http://google.fr">) - I tell JS to go and find
testand add this element to the<p>tag which hastestas an ID.
Why it is not working?
Try adding some content to the element as well by setting the
.innerHTMLproperty. As it is now you are just adding an empty anchor. Try something like this: