Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

javascript - How do I append results of for loop to table?

I have an array:

let items = document.getElementsByClassName('class-name')

for (let i = 0; i < items.length; i++ ) {
    status = items[i].innerText;
    console.log(status)
}

This returns either "SOLD" or "" depending on whether the item has been sold or not (it's an e-commerce page I'm looking at).

How do I append these results to a table?

I need a table with each item and whether it's been sold or not.

question from:https://stackoverflow.com/questions/65643001/how-do-i-append-results-of-for-loop-to-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this way

const myTable = document.body.appendChild(document.createElement('table'))

document.querySelectorAll('.class-name').forEach( status => {
  let newRow = myTable.insertRow()
  newRow.insertCell().textContent = status.textContent
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...