Reputation: 4886
I've created a application for converting the HTML table to JSON. the code works fine, but say for example if the td
or th
contains inner components such as span or other child elements , we have to iterate within that to get the real text, in my application I've wrote for getting values if there is one span components, but in other situation if there is more than one components how can we get the real values inside the td and th of table, here we are considering only text values
Upvotes: 0
Views: 79
Reputation: 5226
Use this
and .text()
instead of .innerHTML
( as you are using jquery )
Change
$('td', tr).each(function(j, td) {
if(td.innerHTML.indexOf("span") != -1){
var text = $(this).closest('td').find('span').text();
myTr.push($.trim(text));
}
else{
myTr.push($.trim(td.innerHTML));
}
});
To
$('td').each(function() {
myTr.push( $(this).text() );
});
Upvotes: 2