Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would approach this slightly differently; markup your links with <code>data-*</code> attributes and use just one event handler to control them showing/hiding of the associated content.</p> <p>Make your markup something like:</p> <pre><code>&lt;a href="#" class="slide" data-content="#one" data-text="first"&gt;Show first&lt;/a&gt; &lt;a href="#" class="slide" data-content="#second" data-text="second"&gt;Show second&lt;/a&gt; &lt;div id="one" class="content"&gt;one&lt;/div&gt; &lt;div id="second" class="content"&gt;two&lt;/div&gt; </code></pre> <p>Notice ive added 2 attributes:</p> <ul> <li><code>data-content</code> contains the id of the associated content</li> <li><code>data-text</code> contains the suffix used in the link.</li> </ul> <p>Ive also changed both links to have the same class - this could be anything but allows me to target the links so I can attach the same behaviour:</p> <pre><code>$('.slide').on("click",function(){ var $link = $(this); var contentid = $link.data('content'); $('.slide').not($link) .filter(function(){ return $(this).data('contentvisible');}) .trigger('click'); $(contentid).slideToggle(400,function(){ if($(this).is(':visible')){ $link.data('contentvisible',true).text('Hide ' + $link.data('text')); } else{ $link.data('contentvisible',false).text('Show ' + $link.data('text')); } }); }); $('#one').hide(); $('#second').hide(); </code></pre> <p>Live example: <a href="http://jsfiddle.net/htn3S/1/" rel="nofollow">http://jsfiddle.net/htn3S/1/</a></p> <p><strong>Edit</strong>: Ive update the above to include the functionality of hiding other visible elements before showing one.</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