I am trying to push a div in an array in JavaScript
using template literals, let me give you an example.
var myArray = [];
myArray.push(<div class="example">This is an example</div>);
and then I can access the div in the array and display its content. But the statements after this line in my JavaScript
acts weird. which make me wonder if I am doing the right thing. Is anything wrong with the way I am pushing my div
into the array
?
Add the single quote in the argument:
myArray.push('<div class="example">This is an example</div>');
you are missing quotes, you have to use it as a string:
var myArray = [];
myArray.push('<div class="example">This is an example</div>');
and then push it in some DOM element in order to show it
myArray.push(`<div class="example">This is an example</div>`);
Can use ES6 template literals for inserting a variable or making it dynamic
let array=[];
let variable="This is a example";
array.push(`<div>${variable}</div>`)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.