views:

97

answers:

4

I have my HTML like this:

<table>
<caption class="my_caption">Table 1.1: TABLE CAPTION</caption> 

<tr>...</tr>
<tr>...</tr>
...

I need to get the caption text so I can make some string comparison. I've tried doing .val(), .text(), .html() and .value but none of them work.

Thanks for your help.

EDIT: I actually have a few of those captions. Sorry, I should've mentioned this earlier.

<div>
<table>
<caption class="my_caption">Table 1.1</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table>
<table>
<caption class="my_caption">Table 1.2</caption> 

<tr>...</tr>
<tr>...</tr>
...
</table> </div>

So I have a for -loop that goes through all the captions:

     var cap_tables = $("caption.my_caption");
  for (var i=0;i<cap_tables.length;i++) {
   alert(cap_tables[i].text());
            //i've tried .text(), .html(), .val(), .value to get the caption text 
  }

I will try again will all your suggestions and get back to you guys. Thank you very much!!!

I am using Firefox version 3.5.3

+5  A: 
$('caption').text();

or

$('.my_caption').text();
Darrell Brogdon
A: 

or, if you have a "table" object, your code might look like:

var myTable = $('table');
var myCaptionText = myTable.find('caption').text();
Dave Markle
A: 

This works in the latest Firefox and IE:

jQuery(function(){
    alert( $('caption').html() );
});

Not sure why its not working for you.

cjstehno
A: 

You should try the built in jquery iterator to go over each element rather than that loop you posted.

        $("caption.my_caption").each(function(i,val){
                    alert($(this).text());
                    });
Mark Snidovich
Yes, this worked. Thank you!
Corrine
Hey, that's good!
Mark Snidovich