Like this:
var str = 'blah blah blah';
str += ' blah';
str += ' ' + 'and some more blah';
Answer from karim79 on Stack OverflowYour code is correct. Perhaps your problem is that you are not passing an ID to the AddBorder function, or that an element with that ID does not exist. Or you might be running your function before the element in question is accessible through the browser's DOM.
Since ECMAScript 2015, you can also use template literals (aka template strings):
document.getElementById(`horseThumb_${id}`).className = "hand positionLeft";
To identify the first case or determine the cause of the second case, add these as the first lines inside the function:
alert('ID number: ' + id);
alert('Return value of gEBI: ' + document.getElementById('horseThumb_' + id));
That will open pop-up windows each time the function is called, with the value of id and the return value of document.getElementById. If you get undefined for the ID number pop-up, you are not passing an argument to the function. If the ID does not exist, you would get your (incorrect?) ID number in the first pop-up but get null in the second.
The third case would happen if your web page looks like this, trying to run AddBorder while the page is still loading:
<head>
<title>My Web Page</title>
<script>
function AddBorder(id) {
...
}
AddBorder(42); // Won't work; the page hasn't completely loaded yet!
</script>
</head>
To fix this, put all the code that uses AddBorder inside an onload event handler:
// Can only have one of these per page
window.onload = function() {
...
AddBorder(42);
...
}
// Or can have any number of these on a page
function doWhatever() {
...
AddBorder(42);
...
}
if(window.addEventListener) window.addEventListener('load', doWhatever, false);
else window.attachEvent('onload', doWhatever);
In javascript the "+" operator is used to add numbers or to concatenate strings. if one of the operands is a string "+" concatenates, and if it is only numbers it adds them.
example:
1+2+3 == 6
"1"+2+3 == "123"
javascript - Appending string to variable name - Stack Overflow
How to concatenate variables for this.state
I am trying to send over a variable from one component to another in a different js file
To be honest I'm not sure I follow what you're trying to do. If you're talking about string interpolation, there's a great thing in ES6 called Template Strings.
const clientID = `${vault.clientList}.${vaulNumber}`
Note: Those are Backticks, not single quotes!
More on reddit.comHow can I add query string parameters to my URL?
Check checkbox check state and append to array
Videos
The right was is to use either an array or an object, depending on if the id is going to be sequential or named.
var card_links_grid = {};
var card_id = "the_hanged_man";
card_links_grid[card_id] = "some value";
or
var card_links_grid = [];
card_links_grid.push("some value");
Variable variables are possible, but only with some hard to maintain and debug approaches that should be avoided. Use object types designed for the data structure you want to represent.
I also recommend to use either array or object. But if you want to try with variable variable approach, you can try like this.
var card_id=1;
window['card_links_grid_' + card_id] = 'Marco';
document.write(window['card_links_grid_' + card_id]);
http://jsfiddle.net/Q2rVH/
Ref: http://www.i-marco.nl/weblog/archive/2007/06/14/variable_variables_in_javascri