Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do this:</p> <pre><code>if($('#THIS_DIV','#THIS_PARENT').length == 1) { } </code></pre> <p>By <a href="http://docs.jquery.com/Core/jQuery#expressioncontext" rel="noreferrer">specifying a context</a> for the search (the second argument) we are basically saying "look for an element with an ID of <code>#THIS_DIV</code> within an element with ID of <code>#THIS_PARENT</code>". This is the most succint way of doing it using jQuery.</p> <p>We could also write it like this, using <a href="http://docs.jquery.com/Traversing/find" rel="noreferrer"><code>find</code></a>, if it makes more sense to you:</p> <pre><code>if($('#THIS_PARENT').find('#THIS_DIV').length == 1) { } </code></pre> <p>Or like this, using <a href="http://docs.jquery.com/API/1.2/Traversing/parents" rel="noreferrer"><code>parents</code></a>, if you want to search from the child upwards:</p> <pre><code>if($('#THIS_DIV').parents('#THIS_PARENT').length == 1) { } </code></pre> <p>Any of these should work fine. The <code>length</code> bit is necessary to make sure the length of the "search" is > 0. I would of course personally recommend you go with the first one as it's the simplest.</p> <p>Also, if you are referring to an element by ID it's not necessary (although of course perfectly okay) to preface the selector with the tag name. As far as speed, though, it doesn't really help as jQuery is going to use the native <code>getElementById()</code> internally. Using the tag name is only important when selecting by class, as <code>div.myclass</code> is much, much faster than <code>.myclass</code> if only <code>&lt;div&gt;</code> elements are going to have the particular class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload