On Oct 17, 8:42 am, Chris <matchett...@googlemail.comwrote:
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