javascript create and append element function not work – Education Career Blog

this is my javascript function :

function createLink (){
    var link = document.createElement("a");
    link.href = "#";
    var linkText = document.createTextNode("This is dynamic link");
    link.appendChild(linkText);
    document.body.appendChild(link);
}

window.onload = createLink;

but i am not get any result from this. any one know, what is wrong with my code?

,

What you have works, you can test it here. Make sure that whenever you’re running this isn’t after window.onload has already fired, otherwise it won’t run.

Also, make sure something else isn’t stealing the onload event, and setting the handler to something other than createLink, for example a window.onload = otherFunction later down the line.

Leave a Comment