PDA

View Full Version : Dynamically generated getElementById() label



hexeliebe
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 );
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.

jscheuer1
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">
<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>