Reputation: 413
I am trying to insert some new links using ‘innerHTML’. As there may be a number of calls on the same ‘ids’ I thought it would be wise to use variables. The following does not respond beyond the alert? The process works fine if I don’t use ‘var link’ and just enter it in full. Is there an issue perhaps trying to do this with xhtml?
Thanks.
var newlink = '<a title="new link" href="newlink.htm">New Link</a>';
var link = "document.getElementById('idlink')";
if( link ) {
alert("link confirmed");
link.innerHTML = newlink;
}
Upvotes: 0
Views: 1457
Reputation: 700342
You are assigning a string to the variable. Just because the contents of the string looks like code that could be run doesn't mean that it actually runs. It's just a string.
Call the method and assign the result to the variable:
var link = document.getElementById('idlink');
Upvotes: 1
Reputation: 38147
var link = "document.getElementById('idlink')";
should be
var link = document.getElementById('idlink');
Upvotes: 4