Evaluating an element in a function declaration, not at run time.

I

Ian Skinner

Hopefully this is an easy one, but I'm not sure how to describe it
concise enough to Google.

I have the following bit of JavaScript:

node.childNodes[0].onclick=function()
{
showHideAllDivs
(
navRoot.parentNode.id,
this.innerHTML.replace(/[^A-Za-z]/g,""),
this.id
);
return false;
} [white space modified for e-mail]

This dynamically assigns a function call to the onClick event of
several anchor tags for some DHTML shenanigans.

The trouble is that the first parameter of the showHideAllDivs()
function needs to be the value of navRoot.parentNode.id at the time of
this assignment (navRoot is initialized before this code), but instead
the event is being created with the navRoot.parentNode.id value
un-resolved and it is being resolved at runtime when the anchors are
clicked on. Unfortunately it is then the wrong value and I get errors
that I spent a very long time tracking down to this cause.

There has got to be a way to write this to say use the value of
navRoot.parentNode.id right now, not wait until later to resolve the
value?

TIA
--------------
Ian Skinner
Web Programmer
BloodSource
www.BloodSource.org
Sacramento, CA

"C code. C code run. Run code run. Please!"
- Cynthia Dunning
 
L

Lee

Ian Skinner said:
Hopefully this is an easy one, but I'm not sure how to describe it
concise enough to Google.

I have the following bit of JavaScript:

node.childNodes[0].onclick=function()
{
showHideAllDivs
(
navRoot.parentNode.id,
this.innerHTML.replace(/[^A-Za-z]/g,""),
this.id
);
return false;
} [white space modified for e-mail]

I find it much more readable as:

node.childNodes[0].onclick=function() {
showHideAllDivs( navRoot.parentNode.id,
this.innerHTML.replace(/[^A-Za-z]/g,""),
this.id
);
return false;
}

There has got to be a way to write this to say use the value of
navRoot.parentNode.id right now, not wait until later to resolve the
value?

Instead of:
onclick=function(){...}
use:
onclick=new Function("showHideAllDivs("
+navRoot.parentNode.id+","
+"this.innerHTML.replace(/[^A-Za-z]/g,''),"
+"this.id);return false;");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top