Update function variable on same page

Discussion in 'Javascript' started by Chris, Oct 16, 2008.

  1. Chris

    Chris Guest

    I have a snippet of a function below I fire up in the body tag of my
    webpage to show a hidden layer and do some stuff when any link with
    the name "showlink" is clicked.

    In the displayed layer there is some html and more javascript. How
    would I update the value of "str" with "node_id" each time a link is
    clicked? I can't figure it out...

    Many thanks,

    Chris


    // function in body tag
    function showStuff(evt,txt){
    var node = (evt.target) ? evt.target : ((evt.srcElement) ?
    evt.srcElement : null );

    if (node.getAttribute("NAME") == "showlink") {
    node_id = node.getAttribute("ID");
    var cfmBox = document.getElementById(frmBox);

    var cfmData = document.getElementById("cfmDataAsset");
    cfmData.setAttribute('value', node_id);
    }
    }
    -----------------------------------------------------------------------------------------------------------------------
    // script in body, hidden layer
    <script type="text/javascript">
    myFunction(str);
    </script>
     
    Chris, Oct 16, 2008
    #1
    1. Advertising

  2. Chris

    RobG Guest

    On Oct 17, 8:42 am, Chris <> wrote:
    > I have a snippet of a function below I fire up in the body tag of my
    > webpage to show a hidden layer and do some stuff when any link with
    > the name "showlink" is clicked.
    >
    > In the displayed layer there is some html and more javascript. How
    > would I update the value of "str" with "node_id" each time a link is
    > clicked? I can't figure it out...


    The simple way is to declare a global variable and update its value
    when required. You can also use a closure to hold the variable so
    that it becomes private, that way you can control access to read and
    write its value.

    If you have many such variables, you can create a single global object
    that has them as properties. That makes management a lot easier and
    you can create get/set methods of the same object to control access to
    their values. See:

    <URL: http://javascript.crockford.com/private.html >


    > Many thanks,
    >
    > Chris
    >
    > // function in body tag


    I guess you mean in a script element in the body element.

    > function showStuff(evt,txt){
    > var node = (evt.target) ? evt.target : ((evt.srcElement) ?
    > evt.srcElement : null );


    The following should be sufficient:

    var node = evt.target || evt.srcElement;

    >
    > if (node.getAttribute("NAME") == "showlink") {


    If node hasn’t been set to an object that supports the getAttribute
    method, you’ll get an error. The getAttribute method is a bit buggy
    so best not to use it if you don’t have to - access the property
    directly. Also, check node is not undefined before trying to read its
    attributes:

    if (node && node.name == ‘showlink’)


    > node_id = node.getAttribute("ID");



    You can access the id property of node directly as node.id, there is
    no need for getAttribute.

    > var cfmBox = document.getElementById(frmBox);
    >
    > var cfmData = document.getElementById("cfmDataAsset");
    > cfmData.setAttribute('value', node_id);}


    Again, ditch getAttribute, access properties directly:

    if (cfmData) cfmData.value = node.id;

    > }



    --
    Rob
     
    RobG, Oct 17, 2008
    #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. Mohanasundaram
    Replies:
    44
    Views:
    1,099
    Keith Thompson
    Aug 24, 2004
  2. Mug
    Replies:
    4
    Views:
    312
  3. Replies:
    7
    Views:
    247
    Bob Barrows [MVP]
    Nov 11, 2005
  4. AikidoGuy
    Replies:
    11
    Views:
    585
    Seebs
    Nov 21, 2011
  5. Chris
    Replies:
    0
    Views:
    74
    Chris
    Oct 16, 2008
Loading...

Share This Page