views:

97

answers:

3

I have this code working fine with FF and opera but not with safari and chrome.

j(this).parent().parent().find(".box").load('test.html');

where (this) is for example the "a" in the following example html

<div>
   <p><a href="#">clicky</a> to do ajax<p>
   <div class="box">loadhere</div>
</div>

if i do parent only once I'm at the p tag, this prevents me from finding the "box", so i do parent twice to get to the div. how can i get to the div in all browsers?

+1  A: 
$(this).closest('div')

if you want to be more sure you're getting the right one, give the div a class or something.

$(this).closest('div.magicAjaxContainer')
Pointy
will this select box or the grandparent?
Moak
It selects the first parent (working upwards) that matches the given selector. Note that it's kind-of fragile to rely on a very specific DOM structure anyway. You might need to change your HTML because of style or layout issues.
Pointy
This does not work in the OPs example as `<div class="boy">` is not an ancestor of `<a>`.
Felix Kling
+5  A: 

Try:

$(this).parent().siblings('.box');
Darmen
that's how you find an uncle ;)
Moak
A: 

how about

$(this).parent().next('div.box')
FrenchiInLa