Solved

Loop Table Get All Text Not Working Well?

Posted on 2014-10-29
4
115 Views
Last Modified: 2014-10-29
I have a table like so:

  <tbody><tr><td id="589" class="btn btn-primary"><label>tarts</label><tr><td>class: Article</td></tr><tr><td>case: Accusative</td></tr></td></tr></tbody>

And need to get the Text from each element. I have this code which returns too many results like so in FireFox console:

0: Array[3]
    0: "tartsclass: Articlecase: Accusative"  //yes it's run together like this
    1; "class: Article"
    2. "case: Accusative"
    length: 3
1: Array[1]
    0: "class: Article"
    length: 1
Array[1]
    0: "class: Accusative"
    length: 1

My goal is to extract the text and build a Json string somehow...

[my code]
       function getTableData(table) {
            var data = [];
            table.find('tr').each(function (rowIndex, r) {
                var cols = [];
                $(this).find('th,td').each(function (colIndex, c) {
                    cols.push(c.textContent);
                });
                data.push(cols);
            });
            return data;
        }

[Usage]
     getTableData($('#table1 table:last'));
0
Comment
Question by:WorknHardr
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40411372
It's not clear what the array should be like.

http://jsfiddle.net/ub7pzmhj/1/

And the HTML makes no sense either
0
 

Author Comment

by:WorknHardr
ID: 40411553
Yea, the html is dynamic and rough, working on it. I'll try your code snippet tonight. Thx
0
 
LVL 58

Expert Comment

by:Gary
ID: 40411565
The html has nested TR's inside TR's which is invalid html.
0
 

Author Closing Comment

by:WorknHardr
ID: 40412466
thx, also got the table appends working great...
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now