I would like to add a hyperlink to a page using JS like this:
var TopLink=document.createElement("a");
TopLink.setAttribute("href", "#top");
TopLink.innerHTML = "Top";
TopLink.setAttribute("class", "btn btn-default btn-ref");
TopLink.setAttribute("style", "position:absolute; top:280px; right:40px; width:120px; background:yellow;");
document.body.appendChild(TopLink);
However, I need to update the <body>
tag to have an ID value of top
as it isn't there on the page.
I have tried various things, such as:
// using jQuery
$(document).ready(function(){
$("body").attr('id', 'top');
});
// using vanilla js
var body_tag = getElementsByTagName("body");
body_tag.setAttribute("id", "top");
But neither work - how can I set the ID attribute of the body tag?
I had a look here first: assigning ID's to body tag using jquery
But it didn't answer my question.