Skip to content

Stopping callback function firing on page load + allowing callback call function by reference #19

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
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
29 changes: 24 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: false // to stop the callback being submitted on page load
},

_create: function() {
Expand All @@ -70,8 +71,10 @@
},

_initLayout: function() {
// Hide the receiver element
this.element.hide();
// Hide the receiver element - but we can't use .hide() as screenreaders
// cannot find it in tab index so we need to move it off screen
this.element.css({"position": "absolute",
"left": "-3000px"});

// Create our objects: two labels and the button
this.off_label = $("<span>").addClass("switch-button-label");
Expand Down Expand Up @@ -270,7 +273,15 @@
}
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(this.options.trigger_callback === true) {
if(typeof this.options.on_callback === 'function') {
this.options.on_callback.call(this);
}
else if(typeof window[this.options.on_callback] === 'function') {
window[this.options.on_callback].call(this);
}
}
this.options.trigger_callback = true;
}
else {
// Update the underlying checkbox state
Expand All @@ -291,7 +302,15 @@
}
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(this.options.trigger_callback === true) {
if(typeof this.options.off_callback === 'function') {
this.options.off_callback.call(this);
}
else if(typeof window[this.options.off_callback] === 'function') {
window[this.options.off_callback].call(this);
}
}
this.options.trigger_callback = true;
}
// Animate the switch
this.button.animate({ left: newLeft }, 250, "easeInOutCubic");
Expand Down