user4002542
user4002542

Reputation:

How to pick up data- * in the parent from the child

<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.

Upvotes: 1

Views: 39

Answers (1)

AmmarCSE
AmmarCSE

Reputation: 30587

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

Upvotes: 1

Related Questions