views:

902

answers:

5

I would like to select an element inside the td of one of my tables but I don't really understand the syntax. This is what I tried:

$("table > td:#box")

This is a sample of my table structure:

<div id="main">
<div id="today">
    <table id="list" width="100%" cellpadding="4" cellspacing="0" border="0" style="font-size: 10px; border-collapse:collapse;">
    <tr id="109008">
    <td class="tdstd">
    <a class="box" href="link"></a>
    </td>

Or With Chrome's DOM inspector: alt text

+4  A: 

Well, "#box" means a DOM object with the id "box", as in it being a unique ID. You can select that one directly. But your code suggest that you have several elements with the id "box" which you have to change. You should assign a class to your element inside the TD, or if it's unique by being the only DIV or SPAN in the box, you can access it like this:

$("table td .box")

Note that the ">" selector means that TD has to be a direct child of TABLE, and I'm assuming you have at least a TR level in between, so that won't work either. My example above matches every element with the class "box" inside any TD that is a child to any TABLE.

Obviously I would set a class on the table as well, and use something like this:

$("table.boxes td .box")

Just so you don't accidentally selects things outside the scope you want to work in.


You have now added HTML so I'm editing my answer:

$("table#list a.box")
Sandman
+1 but he has an id on the table already which is considerably more efficient anyway - $('#list td a.box') is the "correct" solution here
annakata
Correct, but I usually use the tag name when doing hierarchical selects so that I can see just what part of the page I'm selecting. Had the id been "tablelist", I wouldn't have, but "list" is so general so I could be confused if my jQuery code would run long. But all that said, you are obviously correct :)
Sandman
A: 
 $("table tr td .box")

Should do the trick.

Vincent Ramdhanie
A: 

I'm not sure, but i think, you need $("td#box")...

cupakob
this isn't right - # means ID selector and there is no elment with an ID of "box" let alone a td with such
annakata
at the beginning was postet only this $("table > td:#box") and not a peace of code :)
cupakob
yeah I noticed that - that's why I didn't downvote you, merely commented :)
annakata
i have no problems with downvote...but now is better :D
cupakob
A: 

This selector...

table td a.box

tells jQuery to find the a tag with a class attribute that contains "box". And this a tag must be inside a td that is inside a table.

Josh Stodola
A: 

the most efficient selector would be.

$('#list').find('a.box');

or

$('a.box', $('#list')[0]);

by selecting the table id first you have set your scope to just the table and then you can search for the element that you need with in that table.

the second selector is just the same, you are selecting something and you are giving the scope as a second parameter.

It's just easier to read the first one.

Bjarki Heiðar