Update function variable on same page

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

    Update function variable on same page

    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,t xt){
    var node = (evt.target) ? evt.target : ((evt.srcElemen t) ?
    evt.srcElement : null );

    if (node.getAttrib ute("NAME") == "showlink") {
    node_id = node.getAttribu te("ID");
    var cfmBox = document.getEle mentById(frmBox );

    var cfmData = document.getEle mentById("cfmDa taAsset");
    cfmData.setAttr ibute('value', node_id);
    }
    }
    -----------------------------------------------------------------------------------------------------------------------
    // script in body, hidden layer
    <script type="text/javascript">
    myFunction(str) ;
    </script>
  • RobG

    #2
    Re: Update function variable on same page

    On Oct 17, 8:42 am, Chris <matchett...@go oglemail.comwro te:
    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,t xt){
    var node = (evt.target) ? evt.target : ((evt.srcElemen t) ?
    evt.srcElement : null );
    The following should be sufficient:

    var node = evt.target || evt.srcElement;
    >
    if (node.getAttrib ute("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.getAttribu te("ID");

    You can access the id property of node directly as node.id, there is
    no need for getAttribute.
    var cfmBox = document.getEle mentById(frmBox );
    >
    var cfmData = document.getEle mentById("cfmDa taAsset");
    cfmData.setAttr ibute('value', node_id);}
    Again, ditch getAttribute, access properties directly:

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

    --
    Rob

    Comment

    Working...