Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd class to group specific div on focus with jQuery
    primarykey
    data
    text
    <p>In the following code, I'm trying to make it so that when an input is selected (focused), that the 'formhead' div in the associated fieldset changes its background color, and on blur it changes back. I would like to do this by adding a class to that same div on focus, and having that class removed on blur. jQuery closest is the most similar in concept to what I'm trying to do, but isn't appropriate because it will only target parent divs. Is there something else that would target the nearest div with that class, without affecting the other classes in the other fieldsets? Or do I have to get much more specific and id the formheads, etc?</p> <pre><code>&lt;fieldset&gt; &lt;div class="formhead"&gt;Heading Title 1&lt;/div&gt; &lt;div class="fieldleft"&gt; &lt;label for="specificinput"&gt;Input Title&lt;/label&gt; &lt;input type="text" class="forminput" id="specificinput"&gt; &lt;/div&gt; &lt;div class="fieldleft"&gt; &lt;label for="specificinput"&gt;Input Title&lt;/label&gt; &lt;input type="text" class="forminput" id="specificinput"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;div class="formhead"&gt;Heading Title 2&lt;/div&gt; &lt;div class="fieldleft"&gt; &lt;label for="specificinput"&gt;Input Title&lt;/label&gt; &lt;input type="text" class="forminput" id="specificinput"&gt; &lt;/div&gt; &lt;div class="fieldleft"&gt; &lt;label for="specificinput"&gt;Input Title&lt;/label&gt; &lt;input type="text" class="forminput" id="specificinput"&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p>And the jQuery:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $('input').focus(function( ){ $(this).closest('.formhead').addClass('bluebg'); }); $('input').focus(function( ){ $(this).closest('.formhead').removeClass('bluebg'); }); }); &lt;/script&gt; </code></pre>
    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. 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