1
<header data-ride="share">
 <div class="child__one">
   <div class="child__two">
     <div class="child__three"></div>
   </div>
 </div>
</header>

Is possible to catch the header tag from the child__three??

For example:

var parent = $('.child__three').parents('[data-ride="share"]');

This won't work at all.

Thanks.

1 Answers1

1

Use jQuery closest()

var parent = $('.child__three').closest('[data-ride="share"]');

The problem with parents() is it can return multiple objects.

See Difference between jQuery parent(), parents() and closest() functions

DEMO

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • Can you please show me this in a working fiddle? Because i already try use the closest and the console.log(parent) will show -> [div.child__three, context: div.child__three] –  May 20 '15 at 00:25
  • but if i use the closest('[data-ride]'); then will work.. hm! –  May 20 '15 at 00:27
  • @AmandaFerrari, even if you try var parent = $('.child__three').parents('[data-ride="share"]'); from your question, it works. One sec, I will update my fiddle to show you – AmmarCSE May 20 '15 at 00:29
  • That's so weird.. well.. i'm going to accept your answer because is working in your code, but in mine is not. I'm using prototype pattern and i'm not using the $('.child__three'), i'm using the this keyword (but in the context it means the same thing).. could this causing some problem?? –  May 20 '15 at 00:30
  • 2
    @AmandaFerrari, no you dont have to accept my answer. I want to help. Can you post some of the relevant prototype code? – AmmarCSE May 20 '15 at 00:31
  • Yes, i can. Just give me a second. –  May 20 '15 at 00:32
  • Can you put the code surrounding the 'this' keyword you are using? Even if I cant help, maybe somebody else will – AmmarCSE May 20 '15 at 00:32
  • http://codepen.io/celicoo/pen/rVLEPE there it is @AmmarCSE, the this.parent will be the child and not the parent. –  May 20 '15 at 00:39
  • did you see? @AmmarCSE –  May 20 '15 at 00:47
  • ok :).. This doesn't make much sense i think, i already try to solve with a co-worker but we don't have any idea.. –  May 20 '15 at 00:51
  • 1
    ok, found the problem. You have two attributes for that element with 'data-ride'. Looking for a solution – AmmarCSE May 20 '15 at 00:56
  • 1
    If you look here, it works becuase I changed the order of the 'data-ride' attributes. http://codepen.io/anon/pen/RPRzzg Do you see the problem? – AmmarCSE May 20 '15 at 00:58
  • i get that know.. i have several data-rides with several purposes in my code, its like plugins, but i'm going to change everything :). Thanks for the help!!! –  May 20 '15 at 01:04