Hi Karl.. thanks for the reply...
what i actually wanted is.... to select the first tbody.... thats why i
specified tbody:first-child there....
since a table can have multiple tbody... i want to select the first tbody.
What I didnt understand is.... about the thead..
the following works....
<table id="myTable">
<tbody>
<tr>
....
</tr>
</tbody>
</table>
$("#myTable tbody:first-child tr").length;
but this doesnt work:
<table id="myTable">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
<tr>
....
</tr>
</tbody>
</table>
$("#myTable tbody:first-child tr").length;
I dont know how thead causes the second code above not to work...
On Wed, Feb 25, 2009 at 8:11 PM, Karl Rudd <[email protected]> wrote:
>
> tbody:first-child doesn't select the "first child of the tbody" it
> says "select the tbody that is the 'first-child' of it's parent".
>
> So what you are actually wanting to say is:
>
> $("#myTable tbody tr:first-child")
>
> Which is "select the tr that is the first child of tbody"
>
> http://docs.jquery.com/Selectors/firstChild
>
> Karl Rudd
>
> On Wed, Feb 25, 2009 at 8:08 PM, Alex Wibowo <[email protected]> wrote:
> > sorry.... i should say....
> > "how does that explain the behaviour when there's no thead" (because it
> > works when thead doesnt exist)
> >
> > On Wed, Feb 25, 2009 at 8:06 PM, Alex Wibowo <[email protected]>
> wrote:
> >>
> >> how does that explain the behaviour when there's thead then??
> >>
> >>
> >> On Wed, Feb 25, 2009 at 7:47 PM, David Muir <[email protected]>
> wrote:
> >>>
> >>> It's because tbody:first-child is already selecting the tr, so you're
> >>> effectively doing:
> >>> tbody tr tr (where the first tr is the first child of tbody)
> >>>
> >>> Cheers,
> >>> David
> >>>
> >>>
> >>> Alex Wibowo wrote:
> >>>>
> >>>> Hi all,
> >>>>
> >>>> I have a code that counts the number of rows in a table...
> >>>>
> >>>> the table looks like:
> >>>>
> >>>> <table id="myTable">
> >>>> <thead>
> >>>> ...
> >>>> </thead>
> >>>>
> >>>> <tbody>
> >>>> <tr>
> >>>> ....
> >>>> </tr>
> >>>> </tbody>
> >>>> </table>
> >>>>
> >>>>
> >>>> and my jquery looks like:
> >>>>
> >>>> $("#myTable tbody:first-child tr").length;
> >>>>
> >>>> strange enough.... that always returns 0.
> >>>> but if i remove the thead from the table... then it will return the
> >>>> correct number of rows..
> >>>>
> >>>> or alternatively, i can keep the thead, but use the following instead:
> >>>>
> >>>> $("#myTable tbody tr").length;
> >>>>
> >>>> i.e. without specifying first-child.
> >>>>
> >>>> Can anyone explain this behaviour?
> >>>>
> >>>>
> >>>>
> >>>> THanks a lot!
> >>>>
> >>>>
> >>>>
> >>>> --
> >>>> Best regards,
> >>>>
> >>>>
> >>>> WiB
> >>>>
> >>>
> >>
> >>
> >>
> >> --
> >> Best regards,
> >>
> >>
> >> WiB
> >>
> >
> >
> >
> > --
> > Best regards,
> >
> >
> > WiB
> >
> >
>
--
Best regards,
WiB