JavaScript jQuery select child of closest element jquery select child element,jquery select child by class,jquery

Basically I want to be able to select the div level2 parent from the child level4 div. My application does not has such classes, otherwise I'd just select level2 :)

<div class="level1">
  <div class="level2">
    <div class="level3">
      <div class="level4"></div>
    </div>
  </div>
  <div class="level2"> <!-- this is hidden -->
    <div class="level3">
      <div id="start" class="level4"></div>
    </div>
  </div>
</div>

I start with $('#start') and search for the first parent which is visible, but I'm not seeing a way to return the child of that parent. Searching for $('#start') inside the parent seems very wasteful as I start with a sub child to begin with.

$('#start').closest(':visible') // returns level1
$('#start').closest(':visible').first() // returns the first level2. I can't just use second because the number of level2s can change.
$('#start').closest(':visible').children().each(function(){ /* do some search to check it contains `$('#start')` }) // seems very wasteful.

Another way to look at what I'm trying to say would be; start in the middle, find the outside (the visible element), and move one element in.

Answer:1

You want the .has() method

Description: Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.

$('#start').closest(':visible').children().has('#start');

See fiddle for example.

Answer:2

You say that the classes don't exist...why not add them? It would make thinks much easier to find. The class names don't need to have actual styles associated.

var allLevel4 = $('#start').closest(':visible').find('.level4');
var firstLevel4 = $('#start').closest(':visible').find('.level4')[0];
var secondLevel4 = $('#start').closest(':visible').find('.level4')[1]; //also, #start
Answer:3

The help that I received from other users on this forum didn't help at all ... I have a chart that is generated through google charts API on this link http://imageshack.us/f/9/capturesfn.png/ ...

The help that I received from other users on this forum didn't help at all ... I have a chart that is generated through google charts API on this link http://imageshack.us/f/9/capturesfn.png/ ...

I am using sails.js to create a tag system that has many-to-many relations with an entity called Post. The way I am doing the association is to use a database table to correlate each tagId with the ...

I am using sails.js to create a tag system that has many-to-many relations with an entity called Post. The way I am doing the association is to use a database table to correlate each tagId with the ...

  1. using lib async

Question Is it possible to know if the function was fired by a user event or an async event (like callback) without having the original event parameter? Background I'm trying to determine the event ...

Question Is it possible to know if the function was fired by a user event or an async event (like callback) without having the original event parameter? Background I'm trying to determine the event ...

  1. javascript know event listeners

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...

I am an Admin of a Google Spreadsheet. Several times a week me and other admins have to add new users to the spreadsheet so I thought it would be easier if we could use a custom HTML dialog to do so. ...