View Full Version : Dynamically generated getElementById() label

10-23-2007, 01:11 PM
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 );

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.

10-23-2007, 04:00 PM
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:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<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 );
<span id="blah"></span>