Thanks. This works. I realized what I'm really trying to do is find the first instance of '.target' regardless of whether it's a parent or sibling. Is this possible? The script you suggested will only find it if it's a sibling of the parent.
Thoughts? On Nov 5, 1:23 pm, Karl Swedberg <[EMAIL PROTECTED]> wrote: > If you're using jQuery 1.2, you can do it like this: > > $(".btn").click(function(){ > $(this).parent().prevAll(".target:first").css('color','red'); > > }); > > --Karl > > On Nov 5, 2007, at 3:18 PM, Jonathan Sharp wrote: > > > .find() only searches down the tree (so child elements of .btn). > > You may need some combination of .parents() or .siblings(). > > > Cheers, > > -Jonathan > > > On 11/5/07, nemozob <[EMAIL PROTECTED]> wrote: > > > Hi, I'm trying to target the closets instance of an element with a > > class name target but I'm having trouble figuring out how to do this. > > > So given this HTML > > > <p class="target">how are you?</p> > > <p>hello</p> > > <p class="target">how are you?</p> <-- want to target this paragraph > > <p>I am fine</p> > > <div> > > <div class="btn">button</div> > > </div> > > > I'm trying to use something like the following to find the closest > > previous "target" element to "btn". > > > $(".btn").click(function(){ > > $(this).find(".target:first").css('color','red'); > > }); > > > Any ideas?