Note that there are some explanatory texts on larger screens.

plurals
  1. POApplying jquery function to more than one div?
    primarykey
    data
    text
    <p>The code below will show / hide a div on click. It works perfectly for the first div listed, but doesn't work for any of the others. Is there a way to have the function apply to all the elements with the same class names? It should, of course, only open / close the div to which it's being applied (i.e., clicking on the Second Div toggle button should only open / close the Second Div, and not the others...)</p> <pre><code>// this is the markup &lt;div class="collapsible-item"&gt; &lt;div class="collapsible-item-title"&gt; &lt;div class="item-title-header"&gt; First Div&lt;/div&gt; &lt;img src="/images/expand.png" alt="Expand this section" class="toggle-button"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="display: none;" class="togglethis"&gt; Cras cursus sodales odio, quis consectetur felis ultricies in. &lt;/div&gt; &lt;/div&gt; &lt;div class="separator"&gt;&lt;/div&gt; &lt;div class="collapsible-item"&gt; &lt;div class="collapsible-item-title"&gt; &lt;div class="item-title-header"&gt; Second Div&lt;/div&gt; &lt;img src="/images/expand.png" alt="Expand this section" class="toggle-button"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="display: none;" class="togglethis"&gt; Cras cursus sodales odio, quis consectetur felis ultricies in. &lt;/div&gt; &lt;/div&gt; &lt;div class="separator"&gt;&lt;/div&gt; &lt;div class="collapsible-item"&gt; &lt;div class="collapsible-item-title"&gt; &lt;div class="item-title-header"&gt; Third Div&lt;/div&gt; &lt;img src="/images/expand.png" alt="Expand this section" class="toggle-button"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div style="display: none;" class="togglethis"&gt; Cras cursus sodales odio, quis consectetur felis ultricies in. &lt;/div&gt; &lt;/div&gt; </code></pre> <p>And, this is the jQuery:</p> <pre><code>$(document).ready(function () { $('.toggle-button').toggle( function() { $(this).attr('src', '/images/collapse.png'); $(this).parent().siblings('.togglethis').slideToggle('slow'); }, function() { $(this).attr('src', '/images/expand.png'); $(this).parent().siblings('.togglethis').slideToggle('slow'); } ); $('.item-title-header').toggle( function() { $('.toggle-button').attr('src', '/images/collapse.png'); $(this).parent().siblings('.togglethis').slideToggle('slow'); }, function() { $('.toggle-button').attr('src', '/images/expand.png'); $(this).parent().siblings('.togglethis').slideToggle('slow'); } ); }); </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.
    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