Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable a jQuery .toggle() method

Tags:

jquery

I am looking for a way to disable a .toggle() attached to a div

if (condition) {
    $("#div").toggle(
        function() {
            do this
        },
        function() {
            do that
        }
    );
}
else {

    // How do I achieve this?
    $("#div").disableToggle(

    );
}

More explanation:

This is a map application. The toggle in question is bound to an image button. The map loads at zoom level 4. The user zooms in, and the button in question becomes active only when the user is zoomed in more than zoom level 6. Once the user zooms out to less than level 6, I want the toggle button to become inactive again.

like image 279
punkish Avatar asked Sep 28 '11 22:09

punkish


People also ask

How do I disable the toggle button?

To disable a toggle switch, add the disabled attribute to the toggle's checkbox input. When disabled, the toggle appears gray. The user cannot change the state of the toggle. And the present state is reflected in both the location of the toggle switch and by the “on” state being a slightly darker gray.

How do I toggle and off in jQuery?

jQuery toggle() MethodThe toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

What does the toggle method in jQuery do?

jQuery toggle() Method The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on.

How do I toggle a div using jQuery?

To toggle a div visibility in jQuery, use the toggle() method. It checks the div element for visibility i.e. the show() method if div is hidden. And hide() id the div element is visible. This eventually creates a toggle effect.


2 Answers

Why not just unbind the click?

$('#div').unbind('click');
like image 91
Finbarr Avatar answered Oct 13 '22 00:10

Finbarr


You could put condition inside of the function:

$("#div").toggle(
    function() {
        if (condition) {
            //do this
        }
    },
    function() {
        if (condition) {
            //do that
        }
    }
);
like image 29
nachito Avatar answered Oct 13 '22 01:10

nachito