Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://api.jquery.com/hide/" rel="nofollow"><code>hide()</code></a> and <a href="http://api.jquery.com/show/" rel="nofollow"><code>show()</code></a> in jQuery take a <code>duration</code> as the first argument.</p> <p>There is a <code>slideDown()</code> and <code>slideUp()</code> function but I don't think that's what you want. </p> <p><a href="http://jsfiddle.net/Cwalkdawg/AgkR6/20/" rel="nofollow">Check out this fiddle</a></p> <h3>New Markup</h3> <pre><code>&lt;input type="checkbox" id="image1" class="imageBox" name="image1" checked&gt;image 1&lt;/input&gt; &lt;input type="checkbox" id="image2" class="imageBox" name="image2"&gt;image 2&lt;/input&gt; &lt;div id="image1Container" class="imageContainer" style="display:none"&gt;image 1&lt;/div&gt; &lt;div id="image2Container" class="imageContainer" style="display:none"&gt;image 2&lt;/div&gt; </code></pre> <h3>Corresponding code</h3> <pre><code>(function () { var showImgBlock = function (img, container) { (img.is(":checked")) ? container.show(500) : container.hide(300); }; $(".imageBox").each(function () { item = this.id; showImgBlock($("#" + item), $("#" + item + "Container")); }).click(function () { item = this.id; showImgBlock($("#" + item), $("#" + item + "Container")); });; })(); </code></pre> <p>What happens is now your check boxes have a class (<code>.imageBox</code>). When the document loads, each <code>.imageBox</code> is gone through to check the <code>id</code> of that specific check box where the name of the corresponding <code>div</code> container is pulled.</p> <p>I think creating a function would be beneficial. This way you don't need to keep typing the same <code>if/else</code> block over and over. Remember, <a href="http://en.wikipedia.org/wiki/Duplicate_code" rel="nofollow">duplicate code is bad</a></p> <p>Also, I don't know if it's outside your project's scope or not, but in my Fiddle, I used <code>checkbox</code> rather than <code>radio</code> that way you can deselect them as well. Change it, keep it, up to you.</p>
 

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