By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Update function variable on same page

P: n/a
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>
Oct 16 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Oct 17 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.