1

I need to do a simple slide toggle when an element is clicked, however the event needs to use the .on event and I also need to only collapse the element to a specific height, whilst allowing for any height when expanded.

In other words I can't figure out how to implement this: https://stackoverflow.com/a/4965021/162642

Into this:

$(document).on("click",'.SomeElement',function(e)
{
    e.preventDefault();
    // toggle here
}

Any help would be greatly appreciated!

Community
  • 1
  • 1
rich
  • 1,224
  • 1
  • 19
  • 36
  • 1
    You really should have a look at http://api.jquery.com/category/effects/sliding/ and read some before trying to do alot of magic, this will help you in the future, ALOT! – WeeklyDad Mar 20 '13 at 11:45
  • True, however this is a one off thing. I don't tend to do *that* much jQuery. Shall take a look though. – rich Mar 20 '13 at 11:47
  • You can't `click` and `slideToogle` on the same element. There's a phase when you have nothing to click. –  Mar 20 '13 at 11:56

1 Answers1

6

Try this:

$(document).on("click", ".SomeElement", function() {
  if($(this).hasClass("toggled")) {
    $(this).animate({"height": "100px"}).removeClass("toggled");
  } else {
    $(this).animate({"height": "50px"}).addClass("toggled");
}
Pixelomo
  • 6,373
  • 4
  • 47
  • 57
bipen
  • 36,319
  • 9
  • 49
  • 62