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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Skinner

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

    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=functi on()
    {
    showHideAllDivs
    (
    navRoot.parentN ode.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.parentN ode.id at the time of
    this assignment (navRoot is initialized before this code), but instead
    the event is being created with the navRoot.parentN ode.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.parentN ode.id right now, not wait until later to resolve the
    value?

    TIA
    --------------
    Ian Skinner
    Web Programmer
    BloodSource

    Sacramento, CA

    "C code. C code run. Run code run. Please!"
    - Cynthia Dunning
  • Lee

    #2
    Re: Evaluating an element in a function declaration, not at run time.

    Ian Skinner said:[color=blue]
    >
    >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.childNode s[0].onclick=functi on()
    >{
    > showHideAllDivs
    > (
    > navRoot.parentN ode.id,
    > this.innerHTML. replace(/[^A-Za-z]/g,""),
    > this.id
    > );
    > return false;
    >} [white space modified for e-mail][/color]

    I find it much more readable as:

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

    [color=blue]
    >There has got to be a way to write this to say use the value of
    >navRoot.parent Node.id right now, not wait until later to resolve the
    >value?[/color]

    Instead of:
    onclick=functio n(){...}
    use:
    onclick=new Function("showH ideAllDivs("
    +navRoot.parent Node.id+","
    +"this.innerHTM L.replace(/[^A-Za-z]/g,''),"
    +"this.id);retu rn false;");





    Comment

    Working...