Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to check if an element that satisfies my conditions is the first one of a kind inside its container?
    primarykey
    data
    text
    <p>Consider this markup:</p> <pre><code>&lt;div class="click"&gt;click&lt;/div&gt; &lt;div class="container"&gt; &lt;div&gt; &lt;div data-attr="color"&gt;blue&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt;&lt;!-- this is the only n-th child div to be red --&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="click"&gt;click&lt;/div&gt; &lt;div class="container"&gt; &lt;div&gt; &lt;div data-attr="color"&gt;blue&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt;&lt;!-- this is the only n-th child div to be red --&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>and this script:</p> <pre><code>&lt;script&gt; $(document).ready(function () { $('.click').click(function () { $('.container div').each(function (i) { if ($(this).attr('data-attr') == "color" &amp;&amp; $(this).html() == "red" &amp;&amp; $(this).prevUntil('.container').filter(function (i) { return $(this).attr('data-attr') == "color" &amp;&amp; $(this).html() == "red" }).length == 0) { $(this).css('color', 'red'); } }); }); }); &lt;/script&gt; </code></pre> <p>That is, I want matching element to be red only if it's first. My problem is I'm restricted to use a script of another structure, so only <code>ThisIsFirstInsideItsClosestParentContainer</code> part within <code>if()</code> must be rewritten, and the rest part of this script must stay intact.</p> <pre><code>&lt;script&gt; $(document).ready(function () { $('.click').click(function () { $('div').filter(function (i) { return $(this).attr('data-attr') == "color" &amp;&amp; $(this).html() == "red" }).each(function (i) { if (ThisIsFirstInsideItsClosestParentContainer) { $(this).css('color', 'red'); } }); }); }); &lt;/script&gt; </code></pre> <p>Besides, I strongly dislike the use of prevUntil() in my case, but I haven't found another way. So, what's that <code>ThisIsFirstInsideItsClosestParentContainer</code> should look like, without using prevUntil()?</p> <p><strong>EDIT</strong><br> Please, tell my why this doesn't work</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function () { $('.click').click(function () { $('.container [data-attr]').filter(function (i) { return $(this).attr('data-attr') == "color" &amp;&amp; $(this).html() == "red" }).each(function (i) { if ($(this).index() == 0) { $(this).css('color', 'red'); } }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="click"&gt;click&lt;/div&gt; &lt;div class="container"&gt; &lt;div&gt; &lt;div data-attr="color"&gt;blue&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt;&lt;!-- this is the only n-th child div to be red --&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="click"&gt;click&lt;/div&gt; &lt;div class="container"&gt; &lt;div&gt; &lt;div data-attr="color"&gt;blue&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt;&lt;!-- this is the only n-th child div to be red --&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;div data-attr="color"&gt;red&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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