Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Loop Table Get All Text Not Working Well?

Posted on 2014-10-29
4
Medium Priority
?
120 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

972 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