Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loop Table Get All Text Not Working Well?

Posted on 2014-10-29
4
Medium Priority
?
119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
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

722 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