Click to See Complete Forum and Search --> : Does javascript offer updatable parameters
cupboy
09-03-2008, 04:03 PM
When calling a function can a parameter be updated? I tried adding the word ref in front, but Javascript didnt like that idea. I also tried an ampersand. Is it just not possible to update a parameter in a function with Javascript?
JPnyc
09-03-2008, 09:29 PM
if the parameteris being passed as an argument yes, or if it's a global variable, you can actually set the value in the function call as well.
cupboy
02-17-2010, 03:59 PM
Doesn't work if I try it. The result is "blank" when I would like it to be "stuff here".
<?php
print "Testing <br />";
?>
<script>
function updthis(input,output)
{
output = 'stuff here';
}
var var1 = 'blank';
updthis('does it work',var1);
alert(var1);
</script>
johanafm
02-18-2010, 09:03 AM
Since you only seem to need one return value, simply returning it is usually preferable.
var1 = updthis(input);
And if you need multiple return values, you can always return arrays or objects.
But yes, it can be done, just not in that way you tried it
var var1 = 'blank';
function updateGlobal(input) {
var1 = input;
alert('in stickToLocal(): ' + var1);
}
function stickToLocal(input) {
var var1 = input;
alert('in stickToLocal(): ' + var1);
}
function runTest() {
stickToLocal('first');
alert(var1);
updateGlobal('second');
alert(var1);
}
</script>
</head>
<body onload="runTest();">
Weedpacket
02-18-2010, 07:19 PM
And if you need multiple return values, you can always return arrays or objects.Or put them in an array, pass the array, and change the array's elements. (I suggest that merely as an option, not as a preference: I prefer to use return values to return values.)
PHP Builder
Copyright Internet.com Inc. All Rights Reserved.