const array = [
{
text: "hello",
number: 23,
},
];
const container = document.querySelector(".container");
const btn = document.querySelector(".btn");
btn.addEventListener("click", createText);
function createText() {
const newText = document.createElement("h1");
newText.innerText = `${array.text}`;
container.appendChild(newText);
}
i want to get value text from array? what is wrong? why i get undefined ??
2 Answers 2
Your this code needs to be changed to
newText.innerText = `${array[0].text}`;
answered Jun 29, 2020 at 3:53
Anku Singh
9546 silver badges12 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Anku Singh
Hey i haven't why will i do that
andreas rikardo
what do you mean bro? I do not understand, sorry I just new joined this site xD ..
array[0].text
But you should change the structure of "array"... For the moment you have an array with only one element, and it's an object.
Either you want to handle an array of objects, or you want to only have one element, in that case you use an object:
const case1 = [
{
text: "hello",
number: 23,
},
{
text: "hello2",
number: 2345,
}
];
const case2 = {
text: "hello",
number: 23,
}
answered Jun 29, 2020 at 3:51
djcaesar9114
2,1472 gold badges30 silver badges51 bronze badges
3 Comments
andreas rikardo
thank you so much, it work ... is the array writing wrong?
andreas rikardo
oo i see xD .. thnk you one more time for the insight
andreas rikardo
ohh, I understand now, thank you so much, I learned a lot here
default