Update function variable on same page

C

Chris

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);
}
}
 
R

RobG

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:

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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top