You can insert your entire HTML inside of a backtick string and interpolate values like this:

let popupContent =
  `<div id= "divTrophy">
    <img src=${trophy}></img>
    <button onClick="trophyCollection(${trophy})">Collect trophy</button>
    <button onclick="closePopUp()">Close pop up</button>
  </div>`
Answer from Alex Okarkau on Stack Overflow
๐ŸŒ
OutSystems
outsystems.com โ€บ forums โ€บ discussion โ€บ 46385 โ€บ how-to-concatenate-a-string-to-html-string
How to concatenate a string to HTML string? | OutSystems
I want to pass dynamic variables (StringList) into the HTML string to be rendered. For example: ยท But doing it this way the Html is not rendered
Top answer
1 of 5
87

Your 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);
2 of 5
40

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"
๐ŸŒ
Ionic Framework
forum.ionicframework.com โ€บ ionic framework โ€บ ionic-v3
How can I concatenate two variables in HTML? - ionic-v3 - Ionic Forum
November 19, 2017 - Hi, Any idea how to concatenate two variable into one value ? <ion-input formControlName="iNom" type="text" value="{{userDetails.first_name}}{{userDetails.last_name}}" [(ngModel)]="userData.NomConducteur"></ion-input>
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-concatenate-multiple-string-variables-in-JavaScript
How to concatenate multiple string variables in JavaScript?
In the below example, we have used the + and $ operator to concatenate the multiple string variables with the normal string. <html> <head> </head> <body> <h2>Concatenate multiple string variables in JavaScript.</h2> <h4>Concatenating the "Welcome " "To " "The " "TutorialsPoint ", using the ...
Find elsewhere
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Learn_web_development โ€บ Core โ€บ Scripting โ€บ Strings
Handling text โ€” strings in JavaScript - Learn web development | MDN
How numbers should be displayed as strings is fairly well-defined, so the browser automatically converts the number to a string and concatenates the two strings. If you have a numeric variable that you want to convert to a string or a string variable that you want to convert to a number, you ...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_string_concatenate.asp
PHP - Concatenate Strings
... An easier and better way is by using the power of double quotes. By surrounding the two variables in double quotes with a white space between them, the white space will also be present in the result:
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ js โ€บ string_concat.php
JavaScript: String concat() method
You can also use the + operator to concatenate values together. Let's take a look at an example of how to use the concat() method in JavaScript. ... var totn_string = 'Tech'; console.log(totn_string.concat('On','The','Net')); console.log(totn_string); In this example, we have declared a variable ...
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ fundamentals โ€บ string-concat
3 Ways to Concatenate Strings in JavaScript - Mastering JS
You can concatenate strings in JavaScript using the `+` operator, the `Array#join()` function, or the `String#concat()` function. Here's what you need to know.
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 70789-how-to-concatenate-variables-into-a-string-in-javascript
[JavaScript] - How to concatenate variables into a string in JavaScript
Learn how to use the plus operator to concatenate variables into a string in JavaScript. ... Im learning about the AJAX thing in the API thing and I am so confused. I'm really confused about how API exactly works and what exactly AJAX and Axios ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-concatenate-two-strings-so-that-the-second-string-must-concatenate-at-the-end-of-first-string-in-javascript
How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?
We will concatenate simple string values by using the concat() method with just an empty string variable. <!DOCTYPE html> <html> <title>How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript - TutorialsPoint</title> <head> <meta ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ concat
String.prototype.concat() - JavaScript | MDN
This feature is well established and works across many devices and browser versions. Itโ€™s been available across browsers since July 2015. ... The concat() method of String values concatenates the string arguments to this string and returns a new string.