nobody
nobody

Reputation: 8293

Jquery Selector for Table Data with Title

I have a table with a class and a few table rows with classes (all of these are classes since multiple similar tables are generated from the same code) and I want to get some information out of the table data contained therein that has a title. It looks something like this:

<table class='mytable'><br/>
<tr>< td></td></tr><br/>
<tr class ='useful'><td title='hasTitle'>9</td></tr><br/>
</table><br/>

(added in spaces to the html because i'm not sure how to get it to display the tags..)

Is there a way in jquery to select the 9 (so the inner text of the table data) based on the table class and the table row and td title? I would assume so but I'm not sure. I'm guessing it would look something like

  $('#mytable tr#useful td[title='hasTitle']).html(); 

but I am not sure if that selectors syntax is correct or not.

Upvotes: 1

Views: 13631

Answers (3)

thecodeparadox
thecodeparadox

Reputation: 87073

$('.mytable tr.useful td[title="hasTitle"]').html();

As you're note to class, not id for table, so you should use . instead of #.

Read about jQuery id selector and class selector

Upvotes: 1

Damien Locque
Damien Locque

Reputation: 1809

The selector jquery is like "CSS" you write :

$('#mytable tr#useful td[title='hasTitle']).html(); 

Here you're looking for an object with the ID "mytable" with a tr inside ....

You should write something like :

$('table.mytable tr.useful td[title="hasTitle"]').html();

Upvotes: 0

j08691
j08691

Reputation: 207943

$('.mytable tr.useful td[title="hasTitle"]').html()

jsFiddle example.

You're referencing ID's when you should be referencing classes. # selects an ID while . selects a class.

An alternative would be to use the code you have (with a small quote correction so it was $('#mytable tr#useful td[title="hasTitle"]').html();), and change the HTML of your table to:

<table id='mytable'><br/>
<tr><td></td></tr><br/>
<tr id='useful'><td title='hasTitle'>9</td></tr>
</table>​

Upvotes: 0

Related Questions