MTAG11
MTAG11

Reputation: 396

How do I find a table of certain class with jquery selectors

How do I select just a table with class d with jquery selectors. For some reason this code won't work properly...

var dTableTags = $(".d table");

example table would be...

<table id="thetable" class="d">
  <thead>
    <tr><th>Column 1 header</th><th>Column 2 header</th></tr>
  </thead>
  <tbody>
    <tr><td>Column 1 data</td><td>Column 2 data</td></tr>
  </tbody>
</table>

Upvotes: 6

Views: 32795

Answers (3)

Justin Russo
Justin Russo

Reputation: 2214

If you have an ID property/attribute set on the element, why are you selecting by class, anyways? Selecting by ID is much better if you're trying to select something in a distinct fashion. CSS classes can be shared, but the ID property, not so much.

Thus, your selector should be $('#thetable')

Anything else is superfluous really.

Upvotes: 0

Richard Ev
Richard Ev

Reputation: 54087

Your selector is wrong; try $("table.d") instead.

The jQuery documentation does not explain this directly, it defers to the W3C CSS selector documentation which is a lot more comprehensive.

Upvotes: 18

Sushanth --
Sushanth --

Reputation: 55740

You are trying to search for table that is inside class d Which is wrong ..

Change your selector to this

$("table.d");   // Because the table has the class d

Upvotes: 3

Related Questions