1

I have a filter that I toggle.

When i click on said area, it toggles the size of filterarea correctly.

What I want to be able to do is close it using another button too.

Setting the height to 15 in another method works, but it assumes the toggle state is not changed, so you have to click it a few times to cycle through to so its in sync.

Is there any way to just tell the filter toggle to occur. Instead of closing it seperately.

 $("#filtertoggle").toggle(function () {
                $("#filterarea").animate({ height: "200px" }, 400);
            },
            function () {
                $("#filterarea").animate({ height: "15px" }, 400);
            });

Calling the below is actually just an unparmeterised toggle, so it will just hide filtertoggle. It doesnt call teh method i created.

$("#goFilter").click(function () {
    $( ".filtertoggle" ).toggle();
}
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • 5
    As a sidenote, in newer versions of jQuery the toggle method used that way is deprecated and removed. – adeneo Sep 26 '13 at 14:22

2 Answers2

2

try this:

$("<element to trigger>").trigger("click");

also, it is very easy to implement your own toggle mechanism - then you will have unlimited control.

lot
  • 1,434
  • 18
  • 23
2

Use this code

var i = 0;
$("#goFilter").click(function () {
    var ht = (i++ % 2 == 0) ? 200 : 15;
    $("#filterarea").animate({
        height: ht + 'px'
    }, 400);
});

and

$("#filtertoggle").click();

http://api.jquery.com/category/deprecated/

http://api.jquery.com/toggle-event/

To generate toggle -event like functionality

DEMO

.one() documentation.

function handler1() {
    alert('First handler: ' + $(this).text());
    $(this).one("click", handler2);
}
function handler2() {
    alert('Second handler: ' + $(this).text());
    $(this).one("click", handler1);
}
$("div").one("click", handler1);

jQuery click / toggle between two functions

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
  • Is this a non-depricated way of doing things? Aka, by having a single function for toggle? – IAmGroot Sep 26 '13 at 14:33
  • Thanks, been reading about it and seen the updates. http://jquery.com/upgrade-guide/1.9/#toggle-function-function-removed Toggle is only depricated to avoid confusion. So while using Jquery 1.7, I don't see any harm in using it. using `.one` in the manner specified actually seems more confusing. attaching new events each time. – IAmGroot Sep 26 '13 at 14:41