5

Hey i dont know if that is possible but i want to set a given variable in js by 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?

5
  • 5
    Do you mean assigning a string reference to the element value so that the value changes when the string changes? Commented Mar 11, 2010 at 9:38
  • 6
    It's not clear to me what about your actual question involves passing by reference. Commented Mar 11, 2010 at 9:43
  • 5
    This is what happens when a question is unclear, you get a lot of mixed answers that are just guessing at the true meaning of what's being asked. Commented Mar 11, 2010 at 9:45
  • 1
    Hmmm.. the question is more about string concatenation, yea? I was puzzled by the "variable by reference". :P Commented Mar 11, 2010 at 12:03
  • "passing variable by reference" has nothing to do with provided example Commented Jul 15, 2011 at 14:00

7 Answers 7

9

Javascript does not support passing parameters by reference.

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

+= works fine.

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

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

stackoverflow

4 Comments

@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)
hmm.. this worked for me document.getElementById("string").value +=string; But this didnt work var sending = document.getElementById("string").value; sending+ = string;
@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.
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.
1

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>

Comments

0

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

3 Comments

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

You can clone the object by first converting it to JSON (watch out for circular references) and then parse it back again. Like so:

function clone(obj) {
  return JSON.parse(JSON.stringify(obj));
}

This uses the internal browser's JSON routines (safer & faster than using an external resource). If you simply must have backwards compatibility you can download the core JSON routines from JSON.org.

Comments

-1

Javascript does not support passing parameters by reference - Not true

Actually it does. Prototyping makes it possible to create true references to all kinds of javascript objects, including strings.

By true reference I mean when:

  • Changes made to a variable or object passed by reference is reflected on the actual variable beeing passed
  • The change is seen by all references to the same variable
  • Everyone knowing the reference may change the referenced variable

To create a true reference to a variable:

  • Create a constructor function
  • Prototype your reference variables on the constructor
  • DO NOT declare variables with the same name inside the constructor function!
  • Create a change() function which changes the prototyped variable or do so directly
  • Optional: Create a change() function-reference inside your constructor which is set to the ChangeRef() function uppon creation

Changes made this way will be seen and may be changed by all other TestRef() objects

function TestRef(s) {
    this.string = 'Own test-string: ' + s;
    this.change = ChangeRef;
}
function ChangeRef(s) {
    TestRef.prototype.refStr = s;
    return TestRef.prototype.refStr;
}
r = 'RefStr';
TestRef.prototype.refStr = r; // PROTOTYPE => 'RefStr', copy of r

s = new TestRef('s'); // Obj.string = Own test-string: s, Obj.refStr = RefStr
o = new TestRef('o'); // Obj.string = Own test-string: o, Obj.refStr = RefStr
ChangeRef('ChangedStr');  // Change referenced string!
TestRef.prototype.refStr; // => ChangedStr, referenced string changed
r; // => RefStr, original string intact
x = new TestRef('x'); // Obj.string = Own test-string: x, Obj.refStr = ChangedStr. New sees changed string
s; // => Obj.string = Own test-string: s, Obj.refStr = ChangedStr. Old sees changed string
o; // => Obj.string = Own test-string: o, Obj.refStr = ChangedStr. Old sees changed string
s.change('Changed by local function');
x; // => Obj.string = Own test-string: o, Obj.refStr = Changed by local function

1 Comment

Using a reference != pass by reference. What we mean by true pass by reference, e.g. in C++, is you can pass a variable into a function (not create some "reference" object; but pass the variable directly), and the function can assign to that variable (not call some method on it; assign) as if the variable was assigned to in the original scope. This is not possible in JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.