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

Discussion in 'Javascript' started by Ian Skinner, Oct 1, 2004.

  1. Ian Skinner

    Ian Skinner Guest

    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
    Ian Skinner, Oct 1, 2004
    #1
    1. Advertising

  2. Ian Skinner

    Lee Guest

    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;");
    Lee, Oct 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ovidesvideo
    Replies:
    4
    Views:
    470
    Andrey Tarasevich
    Dec 10, 2004
  2. Angelo Secchi

    Evaluating time

    Angelo Secchi, Dec 13, 2003, in forum: Python
    Replies:
    1
    Views:
    258
    Matej Pivoluska
    Dec 13, 2003
  3. Replies:
    3
    Views:
    4,697
    Diez B. Roggisch
    Jun 12, 2006
  4. Pierre Yves
    Replies:
    2
    Views:
    468
    Pierre Yves
    Jan 10, 2008
  5. Ravi Parimi
    Replies:
    8
    Views:
    105
    Peter Sundstrom
    Jan 26, 2004
Loading...

Share This Page