views:

88

answers:

2

Hello all

I am stuck in a selector problem in JQuery. I have the following html structure ....

<table>
 <tr>
   <td rowspan="3"></td>
   <td></td>
   <td></td>
   <td rowspan="3"></td>
 </tr>
 <tr>
   <td></td>
   <td></td>
 </tr>
 <tr>
   <td rowspan="4"></td>
   <td></td>
   <td></td>
   <td rowspan="4"></td>
 </tr>
 <tr>
   <td></td>
   <td></td>
 </tr>
 ....
</table>

I want to select the first td in a tr having the property rowspan. How can I select this using JQuery selectors.

something like $("table > tr > td[rowspan]") selects all the td's having the rowspan property.

Regards

+4  A: 

<table>s have implicit <tbody>, that is why your selector isn't working. Try:

$("table td[rowspan]")

Now, the first <td> in each <tr> would be:

$("table tr").find("td[rowspan]:first")

Working example (quite messy, but that's your table): http://jsbin.com/aqere/2

Kobi
+1 good catch on tbody
Anurag
I want to select the first <td> in every <tr> that has the attribute rowspan. The code snippet you told still selects all the <td>s.
ShiVik
Even if I throw in the <tbody> tag and add the tag in html, this still doesn't work.
ShiVik
@ShiVik - true, it doesn't work on IE. Works well in Firefox...
Kobi
@Kobi - didn't work in chrome either. I was checking in Chrome that time.
ShiVik
@ShiVik - interesting. It works for me in chrome, by the way. It seems IE rewrites the `<td>`, I can see it when I use the dom inspector. I'll look into that later...
Kobi
+2  A: 

See first-child

$("table td[rowspan]:first-child")

As @kobi said, there is an implicit tbody element that browsers inject. To be very specific, use:

$("table > tbody > tr > td[rowspan]:first-child")
Anurag
Alright. This worked out perfectly. Thanks.
ShiVik
You should still answer Guffa's comment in your question. If the answer to that is yes, then my solution will not work. The code example you gave does not contain his case.
Anurag