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

Jeff Greenberg jeffreyag at earthlink.net
Fri Oct 1 20:32:46 CDT 2004


There are a few ways to do this, but a simple one is to use a static 
property of the function itself, like so:

node.childNodes[0].onclick=function()

{

showHideAllDivs

(

node.childNodes[0].onclick.navRootReference,

this.innerHTML.replace(/[^A-Za-z]/g,""),

this.id

);

return false;

}

node.childNodes[0].onclick.navRootReference=navRoot.parentNode.id;

Here, I've created a static property of the onclick function called 
"navRootReference" that is used to store the value of 
navRoot.parentNode.id at the time the function is created.  By using a 
static property of an already existing function, you avoid dealing with 
globals or uneccessary objects, but you pay a price in clarity (a small 
price, IMO).

This assumes, of course, that "node" is global or accessible in whatever 
scope this function wil be called in (I'm assuming it is from your 
description of what you are doing.)

If not, you can create a separate object and use it insetad of the 
"node.childNodes[0].onclick" function itself to store your values.


--Jeff Greenberg



Ian Skinner wrote:

> 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
>
> Confidentiality Notice: This message including any attachments is for 
> the sole use of the intended recipient(s) and may contain confidential 
> and privileged information. Any unauthorized review, use, disclosure 
> or distribution is prohibited. If you are not the intended recipient, 
> please contact the sender and delete any copies of this message.
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript
>  
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20041001/6aa98d39/attachment.htm>


More information about the Javascript mailing list