Skip to content

Prevent triggering callback functions on page load #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ Here are the available options:

clear: true // Should we insert a div with style="clear: both;" after the switch button?
clear_after: null // Override the element after which the clearing div should be inserted (null > right after the button)

on_callback: undefined, //callback function that will be executed after going to on state
off_callback: undefined, //callback function that will be executed after going to off state
trigger_callback_onload: true // If the callbacks to be triggered on pageload or not.

Styling
-------
Expand Down
12 changes: 7 additions & 5 deletions jquery.switchButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
clear: true, // Should we insert a div with style="clear: both;" after the switch button?
clear_after: null, // Override the element after which the clearing div should be inserted (null > right after the button)
on_callback: undefined, //callback function that will be executed after going to on state
off_callback: undefined //callback function that will be executed after going to off state
off_callback: undefined, //callback function that will be executed after going to off state
trigger_callback_onload: true // If the callbacks to be triggered on pageload or not. true was the behaviour in original code
},

_create: function() {
Expand Down Expand Up @@ -105,7 +106,7 @@
// This will animate all checked switches to the ON position when
// loading... this is intentional!
this.options.checked = !this.options.checked;
this._toggleSwitch();
this._toggleSwitch(this.options.trigger_callback_onload);
},

_refresh: function() {
Expand Down Expand Up @@ -246,7 +247,8 @@
this._toggleSwitch();
},

_toggleSwitch: function() {
_toggleSwitch: function(trigger_callback) {
trigger_callback = trigger_callback == false ? false : true;
this.options.checked = !this.options.checked;
var newLeft = "";
if (this.options.checked) {
Expand All @@ -270,7 +272,7 @@
}
this.button_bg.addClass("checked");
//execute on state callback if its supplied
if(typeof this.options.on_callback === 'function') this.options.on_callback.call(this);
if(typeof this.options.on_callback === 'function' && trigger_callback == true ) this.options.on_callback.call(this);
}
else {
// Update the underlying checkbox state
Expand All @@ -291,7 +293,7 @@
}
this.button_bg.removeClass("checked");
//execute off state callback if its supplied
if(typeof this.options.off_callback === 'function') this.options.off_callback.call(this);
if(typeof this.options.off_callback === 'function' && trigger_callback == true ) this.options.off_callback.call(this);
}
// Animate the switch
this.button.animate({ left: newLeft }, 250, "easeInOutCubic");
Expand Down