jquery mobile referencing a TR table element

Hi there,
I have the following code:

alert($(this).parent().parent().html());

It shows the contents of a row from a table like this :

                    <td>
                        ONE
                    </td>
                    <td>
                        TWO
                    </td>            
                    <td>
                        THREE
                    </td>

I need to be able to get the values out of the individual <TD> elements. Im dont need a routine to loop through them but simple a line of code to show me how to reference one.

Is it somethng like :

alert($(this).parent().parent('first TD').html());

thanks
           
jellydealAsked:
Who is Participating?
 
nap0leonCommented:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Demo</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript" ></script>
  <script type="text/javascript">
    function demoThis(obj_html){
      var myTDs = obj_html.children('td');
      alert('there are ' + myTDs.length + ' TDs');

      myTDs.each(function(){
        var TDtext = $(this).text();
        alert(TDtext);
      });
    }
  </script>
  <style>
  </style>
</head>
<html>
<table>
  <tr>
    <td>ONE</td>
    <td>TWO</td>
    <td>THREE</td>
  </tr>
  <tr>
    <td>AAA</td>
    <td>BBB</td>
    <td>CCC</td>
  </tr>
  <tr>
    <td colspan="3"><span onclick="javascript:demoThis($(this).parent().parent().parent().children());">click me</span></td>
  </tr
</table>
</html>

Open in new window

0
 
monasCommented:
<table>
  <tr>
     <td>
        ONE
     </td>
     <td>
        TWO
     </td>
     <td id="myTD">
        THREE
     </td>
  </tr>
</table>
<script>alert($("#myTD").parent().children('td').toArray()[0].innerHTML);</script>

Open in new window

0
 
chaitu chaituCommented:
  <table>
  <tr>
     <td>
        ONE
     </td>
     <td>
        TWO
     </td>
     <td id="myTD">
        THREE
     </td>
  </tr>
</table>
<script>alert($("#myTD").siblings('td')[0].innerHTML)</script> 

Open in new window

0
 
jellydealAuthor Commented:
Thanks :))))
0
 
nap0leonCommented:
That was kind of fun.  I found several ways of returning only the first TD's text, but none of them lent themselves to finding the text of the second or third TD.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.