I am trying to append elements with multiple values from my array, but i am doing something wrong. This is my code:
for(var i=0; i < pricesArray.length; i++) {
var ulList = document.getElementById('season-prices');
ulList.append(`
<div class="flex"> <input type="checkbox"></input> <span>` + pricesArray[i].start_date `</span> <span>` + pricesArray[i].end_date `</span>
<span>` + pricesArray[i].currency `</span> <span>` + pricesArray[i].price `</span> </div>
`)
}
The error I get is:
pricesArray[i].start_date is not a function
Is there another way, or a better way to do this? I used to do something like this in jQuery but cannot remember where and how exactly..
asked Aug 28, 2018 at 12:03
Nemanja G
1,8707 gold badges33 silver badges50 bronze badges
-
please show what pricesArray containsGuerric P– Guerric P2018年08月28日 12:06:04 +00:00Commented Aug 28, 2018 at 12:06
-
@YoukouleleY It contains these properties I wrote in the code, start_date, end_date..Nemanja G– Nemanja G2018年08月28日 12:08:13 +00:00Commented Aug 28, 2018 at 12:08
1 Answer 1
you are missing '+' after pricesArray[i].start_date. also after every property. you need to put plus symbol in front and back.
for(var i=0; i < pricesArray.length; i++) {
var ulList = document.getElementById('season-prices');
ulList.append(`
<div class="flex"> <input type="checkbox"></input> <span>` + pricesArray[i].start_date + `</span> <span>` + pricesArray[i].end_date + `</span>
<span>` + pricesArray[i].currency +`</span> <span>` + pricesArray[i].price + `</span> </div>
`)
}
Sign up to request clarification or add additional context in comments.
3 Comments
Denys Séguret
To clarify for everybody: before the back-tick without an operator, a tag function was expected.
Nemanja G
Ah, jesus... but there is another issue now, it is appending these elements as string, not as HTML elements, what should I change for that?
AlexiAmni
try to use : ulList.innerHTML += {your string}
lang-js