Solved

Loop Table Get All Text Not Working Well?

Posted on 2014-10-29
4
108 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
Comment Utility
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
Comment Utility
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
Comment Utility
The html has nested TR's inside TR's which is invalid html.
0
 

Author Closing Comment

by:WorknHardr
Comment Utility
thx, also got the table appends working great...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 41
Create a new template 9 20
C3-->D3 Line Chart 4 16
Css issue on scroll 1 17
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

762 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

10 Experts available now in Live!

Get 1:1 Help Now