wordpress user wordpress user - 4 months ago 6x
jQuery Question

find children while moving upwards in dom

This can be sound little confusing but here it is.

What i want is to find the children(OF CLASS PARENT ) when user clicks on class target.

Important: I am not aware of children class & child inside html structure.Target class can be after 'blah' like in first case OR can be directly after children like in second case.

Information available: class "PARENt" and $(this) [class target]

Find: Children(ID) of class PARENT (you cannot use class .children)

<div class="parent">
<div class="children" id="1">
<div class="blah">
<div class="target">TARGET</div>
<div class="children" id="2">
<div class="target">TARGET</div>
<div class="children" id="3">
<div class="blah">
<div class="target">TARGET</div>


Clicking Target 1 would produce: ID = 1

Clicking Target 2 would produce: ID = 2

Clicking Target 3 would produce: ID = 3


If you want to find only ONE ID use:

$('.target').click(function() {
  var found = false;
  var parent;
  var previous;
  while(!found) {
    if (previous) {
      parent = previous.parent();
    } else {
      parent = $(this).parent();

    if (parent.hasClass('parent')) {
      found = previous;
    previous = parent;