0

I am trying to extract the texts without html markup by using innerText.

//table is a html table structure 
var cells = table.innerText.trim()

The cells data is like the following:

1st cell   


  2nd cell


  3rd cell


  4th cell


  last cell

How do I make every cell data into array?

I have tried

cells = cells.split(' '); 

but it doesn't work.

Any help here? Thanks a lot!

1
  • cells.replace('\n', '').replace('\r','').split(' '); might work Jul 17, 2013 at 18:39

1 Answer 1

4

Just extract the text from each cell:

var arr=[],
    rowCells,
    myRows=table.rows;
for (var i=0; i<myRows.length; i++) {
    rowCells=myRows[i].cells;
    for (var j=0; j<rowCells.length; j++) {
        arr.push(rowCells[j].innerText.trim());
    }
}
3
  • arr.push(d.innerText || d.textContent); Jul 17, 2013 at 18:43
  • @Dementic I am just following the OP who wants to use innerText. btw I prefer (d.textContent||d.innerText) because of Chrome.
    – Christophe
    Jul 17, 2013 at 18:50
  • 1
    you're welcome! Note that not every browser supports innerText (cf. above comments).
    – Christophe
    Jul 17, 2013 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.