Discussion:
Get the previous element matching a class
debussy007
2010-03-02 15:20:00 UTC
Permalink
Hi,

I would like to have the previous TD element with class "time".

I tried : $(this).closest('td').prev('td.time').html() (where "this" is a
div element inside a TD)
But it only works for a div inside a TD that is *directly* following the
td.time element.

See the code below: my jquery selector above is only working for the div's
inside <td class="lun01032010"> (where the latter is directly following
td.time element),
not for the div's inside <td class="mar02032010">

<td class="time">
8am
</td>
<td class="lun01032010">
<div class="quarter daylun"> </div>
<div class="quarter daylun"> </div>
<div class="quarter daylun"> </div>
<div class="quarter daylun"> </div>
</td>
<td class="mar02032010">
<div class="quarter daymar"> </div>
<div class="quarter daymar"> </div>
<div class="quarter daymar"> </div>
<div class="quarter daymar"> </div>
</td>

Thank you for any help.
мэтт
--
View this message in context: http://old.nabble.com/Get-the-previous-element-matching-a-class-tp27757091s27240p27757091.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.
Nathan Klatt
2010-03-02 15:29:38 UTC
Permalink
Post by debussy007
I would like to have the previous TD element with class "time".
I tried : $(this).closest('td').prev('td.time').html()   (where "this" is a
div element inside a TD)
But it only works for a div inside a TD that is *directly* following the
td.time element.
Yeah, prev only gets the immediately preceding sibling; prevAll gets
all previous siblings so I think you want something like this:

$(this).closest('td').prevAll('td.time').last().html()

Nathan

Continue reading on narkive:
Loading...