How to append a string value during assignment in Javascript?
28,576
Solution 1
+=
works fine.
var str = "stack";
str += "overflow";
console.log(str); //alert(str); Use firebug!!
stackoverflow
Solution 2
Javascript does not support passing parameters by reference.
This link offers a good breakdown and some workarounds- Passing by Value or reference
Solution 3
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
Solution 4
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>
Author by
streetparade
Updated on September 13, 2020Comments
-
streetparade over 3 years
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?
-
o.k.w about 14 yearsDo you mean assigning a string reference to the element value so that the value changes when the string changes?
-
T.J. Crowder about 14 yearsIt's not clear to me what about your actual question involves passing by reference.
-
Andy E about 14 yearsThis 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.
-
o.k.w about 14 yearsHmmm.. the question is more about string concatenation, yea? I was puzzled by the "variable by reference". :P
-
Yura Omelchuk almost 13 years"passing variable by reference" has nothing to do with provided example
-
-
T.J. Crowder about 14 yearsWhy wouldn't you use
+=
? Why look up the element multiple times? -
streetparade about 14 yearsHy thanks for the fast answer, unfortunatly the += doesnt work, i dont know why
-
T.J. Crowder about 14 years@streetparade: Yes it does (pastie.org/864704). This suggests the problem lies elsewhere.
-
N 1.1 about 14 years@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)
-
streetparade about 14 yearshmm.. this worked for me document.getElementById("string").value +=string; But this didnt work var sending = document.getElementById("string").value; sending+ = string;
-
T.J. Crowder about 14 years@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. -
user276641 about 14 yearsRemember that a += b is shorthand for a = a + b, it isn't a string version of array.push(foo) which does change array itself.
-
user102008 almost 12 yearsUsing 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.