I’m trying to generate a link (a-element) with JavaScript, but it just doesn’t work. Even though I append the element to the body. Nothing is being displayed. I found various examples, but nothing seems to work.
function getLink(){
var a = document.createElement('a');
a.title = "text";
a.innerHTML = a.title;
a.href = "http://example.com";
document.body.appendChild(a);
}
Looking at the page you linked us to in comments:
getVideois not a function call.Write:
Notice how Vache posted a testcase containing the snippet you gave us, and was able to immediately prove that the function
getVideoworks. Then all it took was finding what other Javascript was involved, and that was just eight characters, leading to this solution. Basic debugging!Further thoughts collated from around the question:
innerTextwould be more appropriate thaninnerHTML; conceptually you’re defining what the user should see, not what markup should produce it. Because of this,innerTextwill also escape HTML entities for you.getVideois a poor name for a function that does not “get” anything at all.