Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with JQuery selectors and "is"
    primarykey
    data
    text
    <p>I'm having difficulty understanding why a particular selector isn't working for me. Admittedly I'm a bit of a JQuery newbie.</p> <p>This correctly selects the <b>first</b> div.editbox on the page and colors it yellow:</p> <pre><code>$('div.editbox:first').css("background-color","yellow"); </code></pre> <p>However, this <code>if ... is</code> construct makes the highlighted border appear for <b>each</b> box as it is moused-over.</p> <pre><code>$('div.editbox').bind('mouseover', function(e) { if ($(this).is('div.editbox:first')) {$(this).css("border", "1px solid red");} }); </code></pre> <p>I have tried variations such as '.editbox :first', '.editbox div:first', etc. </p> <p>Essentially, I want to be able to reliably test whether this is the first or last element of with the class name. </p> <p>Thanks!</p> <p>Edit: here's the HTML I'm using:</p> <pre><code>&lt;body&gt; &lt;div class="container"&gt; &lt;p&gt;&lt;/p&gt; &lt;div class="editbox" id="box1"&gt;Foo&lt;/div&gt; &lt;div class="editbox" id="box2"&gt;Bar&lt;/div&gt; &lt;div class="editbox" id="box3"&gt;Baz&lt;/div&gt; &lt;div class="responsebox" id="rbox"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>This is just a proof-of-concept page; the actual page will of course be much more complex. Again: what I want is to reliably detect if I am in the first or last "div.editbox". A workaround I used is:</p> <pre><code>$('div.editbox:last').addClass("lasteditbox"); </code></pre> <p>Then test for <code>if ($(this).is(".lasteditbox"))</code> which works, but it seems clumsy and I'm trying to learn the proper way to do this with JQuery.</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.
 

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