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.
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.
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.
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.
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.
Why not just unbind the click?
$('#div').unbind('click');
You could put condition
inside of the function:
$("#div").toggle(
function() {
if (condition) {
//do this
}
},
function() {
if (condition) {
//do that
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With