Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  183] [ 6]  / answers: 1 / hits: 5217  / 5 Years ago, mon, august 12, 2019, 12:00:00

I'm a beginner in HTML and JavaScript and I don't know how to include a Javascript variable in an HTML header.
The question is how to define variables in the header of HTML and use them in the body.
For instance,


for (var i in array)
{
var num = i;
var customForm = '<form>
<h2>***INCLUDE NUM VARIABLE HERE***</h2>
</form>';
}

More From » html

 Answers
3

Two ways:



var customForm = '<form> 
<h2>' + num + '</h2>
</form>';


or



var customForm = `<form> 
<h2>${num}</h2>
</form>`


The first one just concats the variable into the string, the second one is template literal (template string) from ES6, and you must use these ` backquotes.


[#6610] Friday, August 9, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quentinaveryb

Total Points: 102
Total Questions: 100
Total Answers: 93

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
quentinaveryb questions
Thu, Aug 6, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
Thu, May 16, 19, 00:00, 5 Years ago
;