Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts

    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;
    }

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    document.getElementById() is a Javascript command and will not work if put into quotes. Just omit the string delimiter quotes around this command!

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Thanks devnull69.

    I started without the quotes, but then I'm not even getting as far as a response to the alert?

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    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.

  • Users who have thanked ckeyrouz for this post:

    Martins (03-02-2012)

  • #5
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    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.

  • #7
    New Coder
    Join Date
    Nov 2005
    Posts
    83
    Thanks
    22
    Thanked 0 Times in 0 Posts
    Thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •