Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am currently trying to implement the show/hide technique using JQuery on my products pages.

The idea:

I want to show all of my product images with a 'Show description' button for each. When this is clicked, the description for the relevant product will be displayed.

Currently, when I click a 'Show description' link for a product, it shows the description div's for all the products on the page. I just want the relevant description to be displayed, not all description div's.

JavaScript in Header:

XML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function(){

        $(".productDescription").hide();
        $(".show_hide").show();

    $('.show_hide').click(function(){
    $(".productDescription").slideToggle();
    return false;
    });

});

</script>



HTML:

XML
<div class="product clearfix">

<h4>Bath Bomb Cake Slice - Melting Heart</h4>

<p>Drizzled with soap this Bath Bomb Cake Slice looks so authentic.</p>

<a href="#" class="show_hide">See Full Description</a>
<div class="productDescription">
<p>This very large bath bomb will fizz up to two minutes, how good is that. Drizzled with soap it looks so authentic. This cake slice has a scent of Tropical Fruit including Pineapple, Mango & Grapefruit and you'll be surrounded by gorgeous flowers and glitter. <a href="#" class="show_hide">Hide</a></p></div>

</div>

<div class="product clearfix">

<h4>Cupcake Bath Bomb - La Vie en Rose</h4>

<p>Muffin Size in it's own Cake Box, this is the most elegant cupcake bathbomb.</p>

<a href="#" class="show_hide">See Full Description</a>
<div class="productDescription">
<p>Muffin Size in it's own Cake Box, this is the most elegant and luxurious handmade cupcake bathbomb bath treat as it contains pure essential oils of lavender and rose. Perfect for those who need to unwind and want a bit of indulgence.<br/>
If you think this Bath-Patisserie creation is too pretty to use, place it on your bathroom shelf and let the aromas fill the room!<br/>
<b>Ingredients:</b>
Sodium Bicarbonate, Citric Acid, Sucrose, Aqua, Egg Powder (Ovum), Prunus Amygdalus Dulcis, Sodium Lauryl Sulphate, Lavandula Officinalis, Rosa damascena, Potassium Bitartrate, Hamamelis Virginiana Distillate, GERANIOL, LIMONENE, LINALOOL, CITRAL, EUGENOL, CITRONELLOL, FARNESOL. <a href="#" class="show_hide">Hide</a></p></div>
</div>
Posted
Updated 28-Jul-16 11:00am

 
Share this answer
 
XML
<h1>
        Show/Hide using jQuery</h1>
    <a id="showhidetrigger" href="#">show/hide</a>
    <div id="showhidetarget">
        This is the box that is hidden and shown.
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#showhidetarget').hide();

            $('a#showhidetrigger').click(function () {
                $('#showhidetarget').toggle(400);
            });
        });
    </script>
 
Share this answer
 
Comments
sanjay_gope 9-Dec-12 23:28pm    
sdsd
XML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){
        $(".productDescription").hide();
        $(".show_hide").show();
        $(".hide_show").hide();

    $('.show_hide').click(function(){
    $(this).parent().find('.productDescription').slideToggle();
         $(this).parent().find(".show_hide").hide();
        $(this).parent().find(".hide_show").show();
    });

 $('.hide_show').click(function(){
    $(this).parent().find('.productDescription').slideToggle();
         $(this).parent().find(".show_hide").show();
        $(this).parent().find(".hide_show").hide();

    });

});

</script>



XML
<div class="product clearfix">
    <h4>Bath Bomb Cake Slice - Melting Heart</h4>
    <p>Drizzled with soap thiauthentic.</p>
    <a href="#" class="show_hide">See Full Description</a>
    <a href="#" class="hide_show">Hide Description</a>
    <div class="productDescription">
       <p>This very </p>
    </div>
</div>

<div class="product clearfix">
    <h4>Cupcake Bath Bomb - La Vie en Rose</h4>
    <p>Muffin Size in it's o cupcake bathbomb.</p>
    <a href="#" class="show_hide">See Full Description</a>
    <a href="#" class="hide_show">Hide Description</a>
    <div class="productDescription">
        <p>Muffin Size in </p>
    </div>
</div>
 
Share this answer
 
v4
Comments
zakaryfaithfull 14-Sep-12 11:16am    
Unfortunately this did not work, it stopped the show/hide from working at all :/
ShotDriller 14-Sep-12 11:37am    
The code that i gave doesnt need Hide anchor. Click on "showDescription" will show the description. click again on the same "showDescription" will hide the Description. You can show "Hide description" anchor tag when description is expanded.
ShotDriller 14-Sep-12 11:48am    
Updated functionality/Code provided. just copy&paste provided code and check. if its what you want.
The best approach for showing/hiding elements is a combination of HTML, CSS and JavaScript. If you want something hidden, find a suitable parent element and assign a "hidden" flag class to it. Then you can easily control which elements inside the parent should be hidden. If you want to unhide then simply remove the "hidden" flag class.

See
http://jsfiddle.net/TEXYa/[^]

The "hidden" flag class is called "hide_description" and is assigned to the "product".

The JavaScript code is then very simple and clear:

JavaScript
$(document).ready(function(){

    $(".show").click(function() {
        $(this).parents(".product").removeClass("hide_description");
    });
    $(".hide").click(function() {
        $(this).parents(".product").addClass("hide_description");
    });
 
});
​


One good thing about this approach is that you can not only hide/show elements but also set any additional attributes based on the "hidden" flag. E.g. if the product or description should use a background image to reflect the shown/hidden state (e.g. plus/minus icon) then you could just update a related CSS for this, without any need to write additional JavaScript code.

Modified HTML code:
HTML
<div class="product clearfix hide_description">
<h4>Bath Bomb Cake Slice - Melting Heart</h4>
<p>Drizzled with soap this Bath Bomb Cake Slice looks so authentic.</p>

<a href="#" class="show">See Full Description</a>
<div class="productDescription">
<p>This very large bath bomb will fizz up to two minutes, how good is that. Drizzled with soap it looks so authentic. This cake slice has a scent of Tropical Fruit including Pineapple, Mango & Grapefruit and you'll be surrounded by gorgeous flowers and glitter. 
<a href="#" class="hide">Hide</a>
</p></div>
</div>


Related CSS:
CSS
.productDescription {display:block;}
.hide_description .productDescription {display:none;}
.show {display:none;}
.hide {display:inline;}
.hide_description .show {display:inline;}
.hide_description .hide {display:none;}
 
Share this answer
 
Comments
[no name] 17-Jan-13 7:31am    
yeah

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900