Alex Man
Alex Man

Reputation: 4886

creation of json with jquery

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

Answers (2)

Rob Sedgwick
Rob Sedgwick

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() );
});

http://jsfiddle.net/d8W9Q/

Upvotes: 2

morepaolo
morepaolo

Reputation: 631

Use jquery's .text() method, look at the reference here:

http://api.jquery.com/text/

Upvotes: 1

Related Questions