0

I have these HTML tags and I need to get the text from the TDs. I already have and can use the ID of li and the div id (li and div (nested in li) IDs are the same).

How to get all the TD text only from one table. The input for template 1 for example should be:

Video, Image

My HTML

<div id="slideHolder">
    <ul id="mainList">
                        <li templateid="1">
                           <div id="1">
                                <table >
                                    <tr>
                                      <td>Video</td>
                                    <td>Image</td>
                                    </tr>
                                </table>
                            </div>
                        </li>
                        <li templateid="2">
                            <div id="2" >
                                <table>
                                    <tr>
                                       <td >Text</td>
                                    <td >Image</td>
                                    </tr>
                                </table>
                            </div>
                        </li>
    </ul>
    </div> 
Damkulul
  • 1,406
  • 2
  • 25
  • 59

3 Answers3

0

You can use:

var tdtexts= $("#mainList td").map(function() {
  return $(this).text();
});

Demo

Update: to find td texts in template 1:

var tdtexts= $("li[templateid='1'] td").map(function() {
  return $(this).text();
});

Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Here is your HTML:

<div id="slideHolder">
    <ul id="mainList" ">
        <li templateid="1">
           <div id="div-id1"  >
                <table >
                    <tr>
                      <td>Video</td>
                    <td>Image</td>
                    </tr>
                </table>
            </div>
        </li>
        <li templateid="2">
            <div id="div-id2" >
                <table>
                    <tr>
                       <td >Text2</td>
                    <td >Image2</td>
                    </tr>
                </table>
            </div>
        </li>
    </ul>
</div> 

And here is the Javascript which retrieves data only from the first table:

var tdtexts= $("#div-id1 td").map(function() {
  return $(this).text();
});

alert("array length: " + tdtexts.length + " first element: " + tdtexts[0] + " second element: " + tdtexts[1]);

See that the result is shown in a popup, not in the HTML.

Also note that you cannot have an integer as an id, as discussed here.

And here is the demo.

Community
  • 1
  • 1
EduardoFernandes
  • 3,091
  • 1
  • 13
  • 12
  • Sorry in this demo I get the text from all tables : here is the input: Video Image Text Image I need only from a specific div/li which has a specific Id – Damkulul Jul 31 '14 at 11:19
  • In this demo you are retrieving data only from the first table since the identifier is "1" in the `$("#1 td")` expression. – EduardoFernandes Jul 31 '14 at 11:49
0

You can find data as table wise from here

$("#mainList table").each(function(){
   $(this).find("td").each(function(){
     return $(this).html();
   });

});