Results 1 to 2 of 2

Thread: Dynamically generated getElementById() label

  1. #1
    Join Date
    Oct 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamically generated getElementById() label

    I am getting old and can't remember how to do this.

    var xy = this.getAttribute('assocSpan') //result is txtmsg1
    var spanTxt = "The contents of this field are too long!";
    var dc=document.getElementById( xy );
    dc.innerHTML=spanTxt;

    It just doesn't work and reports a "NULL" value for xy although when I place an alert just above the third line, the result is correct.

    I need to remember how to generate the getElementById() dynamically because I have associated spans for each form element.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Looks OK to me. If the above is a double quote delimited element event, the string value needs to be delimited by single quotes. Otherwise, there is something else about your page or the code that prevents it from happening.

    Please post a link to the page on your site that contains the problematic code so we can check it out.


    To put it another way, this works:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <input type="text" assocSpan="blah" onchange="var xy = this.getAttribute('assocSpan');
    var spanTxt = 'The contents of this field are too long!';
    var dc=document.getElementById( xy );
    dc.innerHTML=spanTxt;"><br>
    <span id="blah"></span>
    </body>
    </html>
    Last edited by jscheuer1; 10-23-2007 at 04:13 PM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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