0

I created a div that has the full height of it's content set to 500px. First 200px of the 500px is, lets say, a preview. So I set it's height to 200px and overflow: hidden. I then added this script:

<div class="stretcher">
<script type="text/javascript">  
    $('.stretcher').toggle(
        function(){
            $(this).animate({'height': '500px'}, 300);
        }, 
        function(){
            $(this).animate({'height': '200px'}, 300);
        }
    );
</script>

That works but the problem is that I need the contents of the div to be clickable. However, with this script wherever I click it either expands the div or returns it back to the original 200px.

Any idea how I could do it? Maybe adding icons of arrow up and down or something.

Joe
  • 15,205
  • 8
  • 49
  • 56
Dev
  • 79
  • 1
  • 2
  • 11

3 Answers3

4

The toggle() function used that way is deprecated and removed in newer versions of jQuery, use a flag instead :

$('.stretcher').on('click', function(e) {
    if (e.target === this) {
        var h = $(this).height() > 220;
        $(this).animate({'height': ( h ? 200 : 500 ) }, 300);
    }
});

Checking if the target equals this stops any problems when clicking other elements inside .strecher

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • (+1) This is the best way to do it. The `===` ensures that it is an exact match. In this situation this means that if there is another clickable element within the div it will respect that element and not `.toggle` – Kevin Lynch Jun 07 '13 at 12:32
  • this is really good but as ive seen i need to click on edges of div for this to work... anyways i really appriciate help but i used Archers code.. works like a charm since i created half of those upper 200px to be clickable in order to expand or contract. thnx once more – Dev Jun 07 '13 at 14:11
2

Try this...

<div class="stretcher">
    <div class="clickable"></div>
</div>
<script type="text/javascript">  
    $('.clickable').toggle(
        function()
        {
            $(this).parent().animate({'height': '500px'}, 300);
        }, 
        function()
        {
            $(this).parent().animate({'height': '200px'}, 300);
        }
    );
</script>

You have an area called clickable and when you click that it animates the parent container div, but it won't do it when you click the div itself.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
  • thnx m8 ... tihs works for me like a charm.. this clickable div i positioned and puted where i need it and eveyrthing works gr8..thnx – Dev Jun 07 '13 at 14:12
0

You could wrap your code in a function that has an exact match. As @adeneo points out. An example below using your existing code in this manner.

$('.stretcher').on('click', function(e) {
    if (e.target === this) {
        $('.stretcher').toggle(
        function(){
            $(this).animate({'height': '500px'}, 300);
        }, 
        function(){
            $(this).animate({'height': '200px'}, 300);
        }
    );
    }
});
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37