I am trying to extract a user input html data and store them in an object or array
The source of html could be as following
begin <em>texts</em>
description texts here
<table>
<tr><td>cell 1<td><td>cell2</td></tr>
<tr><td>cell 3<td><td>cel4</td></tr>
</table>
<em>second<em> part texts
<table>
<tr><td>cell 5<td><td>cell6</td></tr>
<tr><td>cell 7<td><td>cel8</td></tr>
</table>
I need to be able to store the following things.
all the texts outside of table and the cell data in table. they need to be in order.
I have tried
var data = [];
sourceHtml.contents().each(function(){
if($(this).is('table')){
var table = $(this)
var rows = 'rows:' + $('tr', table).length;
var column ='column:' + $('td', table).length / $('tr', table).length;
//i need to get the table columns and rows counts
data.push(rows)
data.push(column)
table.find('td').each(function(){
data.push($(this).text().trim());
})
}else{
data.push($(this).text().trim());
}
})
The above will only give me all the texts in the sourceHtml but lose all the markup for my texts. Are there anyway to maintain the html markup while getting texts of my data? Thanks so much for the help!
.text()use.html()