Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-02-2012, 12:51 PM   PM User | #1
Martins
New Coder

 
Join Date: Nov 2005
Posts: 68
Thanks: 17
Thanked 0 Times in 0 Posts
Martins is an unknown quantity at this point
document.getElementById as a variable in xhtml

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.

Code:
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;
}
Martins is offline   Reply With Quote
Old 03-02-2012, 01:52 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
document.getElementById() is a Javascript command and will not work if put into quotes. Just omit the string delimiter quotes around this command!
devnull69 is offline   Reply With Quote
Old 03-02-2012, 02:47 PM   PM User | #3
Martins
New Coder

 
Join Date: Nov 2005
Posts: 68
Thanks: 17
Thanked 0 Times in 0 Posts
Martins is an unknown quantity at this point
Thanks devnull69.

I started without the quotes, but then I'm not even getting as far as a response to the alert?
Martins is offline   Reply With Quote
Old 03-02-2012, 02:55 PM   PM User | #4
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
Post full code.

Maybe the HTML tag with id 'idlink' does not exist in your file.

Post the full code so we can help.
__________________
Software and cathedrals are much the same - first we build them, then we pray.
ckeyrouz is offline   Reply With Quote
Users who have thanked ckeyrouz for this post:
Martins (03-02-2012)
Old 03-02-2012, 03:52 PM   PM User | #5
Martins
New Coder

 
Join Date: Nov 2005
Posts: 68
Thanks: 17
Thanked 0 Times in 0 Posts
Martins is an unknown quantity at this point
Thanks ckeyrouz.

This was more or less the problem. I was assigning the 'var link' in the head of the document. If I assign it further down the page after the element it works fine.

Presumably if I put the variable in an external.js this will also have to be loaded after the element?
Martins is offline   Reply With Quote
Old 03-02-2012, 04:14 PM   PM User | #6
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
Yes, you should assign your variable and your code after the load of the page.

You can put either after the element or in a window.onload in an external js file like this:

Code:
window.onload = function() {
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;
}
}
__________________
Software and cathedrals are much the same - first we build them, then we pray.
ckeyrouz is offline   Reply With Quote
Old 03-02-2012, 04:55 PM   PM User | #7
Martins
New Coder

 
Join Date: Nov 2005
Posts: 68
Thanks: 17
Thanked 0 Times in 0 Posts
Martins is an unknown quantity at this point
Thank you.
Martins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:52 PM.


Advertisement
Log in to turn off these ads.