Newbie Question - variable name in getElementById() method doesn't seem to work
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Newbie Question - variable name in getElementById() method doesn't seem to work

From: <spivee@gmail.com>
Date: Mon Jan 30 2006 - 22:11:21 CET

Okay, I'm really new to javascript and the DOM... This isn't a
functional script. I'm not trying to DO anything, just messing around
to see how things work...

I'm trying to call an onMouseOver event, and pass it an element id
(assigned to a DIV element) and a URL, then have my javascript make a
request to the server and retrieve a text document. For some reason,
the first method I pass the variable to can call the
document.getElementById(id) function without problems. But when I try
to pass that 'id' variable to another method, it then sees that
variable as 'undefined'.

Here's my code...

the html tag...

<a href="#" id="myHref"
onMouseOver="doGetUrl('myTest','my.txt')">change content</a><br>
<div id="myTest"></div>

The first method...

function doGetUrl(id,url) {
   var http = createRequestObject();
   sendRequest(url);
     // this changes the div element's contents just fine
   document.getElementById(id).innerHTML = id;
   handleResponse(id);
}

The second method...

function handleResponse(id) {
       // this errors out saying getElementById(id) is undefined.
    document.getElementById(id).innerHTML = 'changed';
       // if I just print the value of 'id' it shows as undefined
       // document.write("id="+id);
       // I do more stuff here to set the inner html for the
       // myTest div to the contents of the txt file
       // if I do document.getElementById('myTest').innerHTML here,
       // it works fine... rest of my code not really relevant here
       // ... i think...
   ....
}

It seems to me that the variable isn't being passed to the second
function correctly. I'm not understanding why.
Received on Tue Feb 7 21:27:55 2006