views:

354

answers:

6

Hy i dont know if that is possible but i want to set a given variable in js per reference.

What i want to do is, that each time i pass a string to the function addstring that the value of the textfield is added like +=

function addstring(string)
{
    document.getElementById("string").value = string; // its a textfield

}

How can i do that?

A: 

document.getElementById("string").value = document.getElementById("string").value + string;

Pentium10
Why wouldn't you use `+=`? Why look up the element multiple times?
T.J. Crowder
Hy thanks for the fast answer, unfortunatly the += doesnt work, i dont know why
streetparade
@streetparade: Yes it does (http://pastie.org/864704). This suggests the problem lies elsewhere.
T.J. Crowder
+1  A: 

+= works fine.

var str = "stack";
str += "overflow";

console.log(str); //alert(str); Use firebug!!

stackoverflow

N 1.1
The += doesnt work for me, i realy dont know why i doesnt
streetparade
@streetparade: show the code where you are adding and the error you are getting. (use firebug(add-on) on firefox or Developer Tools (ctrl+shift+j) if on Chrome)
N 1.1
hmm.. this worked for me document.getElementById("string").value +=string;But this didnt work var sending = document.getElementById("string").value; sending+ = string;
streetparade
it should work. try again :). Something else is afoot here.
N 1.1
@streetparade: Right, because what you've done there is retrieve the value of the text box's content into `sending`, and then adding your string to that value -- which at that point has nothing to do with the text box.
T.J. Crowder
Remember that a += b is shorthand for a = a + b, it isn't a string version of array.push(foo) which does change array itself.
Douglas
+3  A: 

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are completely separate from anything that happens outside the function. Let’s take a look at the following example:

function myfunction(x)
{
      // x is equal to 4
      x = 5;
      // x is now equal to 5
}

var x = 4;
alert(x); // x is equal to 4
myfunction(x); 
alert(x); // x is still equal to 4

Passing in an object, however, passes it in by reference. In this case, any property of that object is accessible within the function. Let’s take a look at another example:

function myobject()
{
    this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6
alemjerus
Objects aren't passed by reference, they are passed **value-by-reference**. If they were passed by reference, you would be able to change the variable itself - not just the properties.
Andy E
*"Passing in an object, however, passes it in by reference."* No, this is a common misconception. JavaScript *only* has pass-by-value. It's just that the value, in the case of an object reference, is an object reference (pointer). That's *completely different* from "pass by reference" which allows you to update the caller's copy of the actual parameter from within the callee. If you can't change the thing being passed in (for instance, to make it point to a *different* object), it's pass-by-value. You can change the properties of the object, but not the value of the caller's reference to it.
T.J. Crowder
This deserves a bigger font.
stereofrog
+1  A: 

If its a reference to an input text filed then you can use the value property

function addstring(string)
{
    document.getElementById("string").value += string.value;

}

See value

rahul
+2  A: 

Javascript does not support passing parameters by reference.

This link offers a good breakdown and some workarounds- Passing by Value or reference

Morten Anderson
+1  A: 

Your code example will work just fine with +=; complete example below. This suggests the problem you're having lies elsewhere.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function addstring(string)
{
    document.getElementById('string').value += string;
}
</script>
</head>
<body><div>
<input type='text' id='string' value=''>
<br><input type='button' value='One' onClick="addstring('one');">
<input type='button' value='Two'     onClick="addstring('two');">
<input type='button' value='Three'   onClick="addstring('three');">
</div></body>
</html>
T.J. Crowder