Skip to content
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

Ability to prevent switching inside event handler #268

Open
raveren opened this issue Mar 18, 2014 · 30 comments
Open

Ability to prevent switching inside event handler #268

raveren opened this issue Mar 18, 2014 · 30 comments
Assignees
Milestone

Comments

@raveren
Copy link

raveren commented Mar 18, 2014

I don't really need this at the moment, this is just for uniformity, other high quality jquery plugins usually implement some way to prevent user from performing actions in event callbacks:

$("[name='my-checkbox']").bootstrapSwitch({
  onSwitchChange: function(e) {
    if( iHaveAReason ) {
       e.preventDefault();
       // or - equivalently
       return false;
    }
  }
});

Returning false from event handler would also stop propagation, but in this case, when switches never contain other switches, that's irrelevant.

@daverogers
Copy link

+1. I was looking for this functionality so I can first display a confirmation to the user before unchecking a particular option that is very important.

@LostCrew
Copy link
Member

LostCrew commented Oct 6, 2014

@daverogers @raveren i opened a branch for it, but it will take some time since it requires many parts to be rewritten.

@mkrauser
Copy link

mkrauser commented Nov 7, 2014

+1 from me. This would be a great improvement! Looking forward to this

@mgoldsborough
Copy link

+1. It would be a huge help. Like @daverogers, I need to a confirmation modal too.

@nbhuy89
Copy link

nbhuy89 commented Mar 20, 2015

+1

4 similar comments
@filipalacerda
Copy link

+1

@dflock
Copy link

dflock commented Mar 30, 2015

+1

@DennisVM
Copy link

+1

@mmunchandersen
Copy link

+1

@nostalgiaz nostalgiaz assigned nostalgiaz and unassigned LostCrew May 15, 2015
@nostalgiaz
Copy link
Collaborator

Can anyone give me feedback before merging this commit in master? 61b2289

@mgoldsborough
Copy link

Works for me! Thanks for the add.

For those looking for how to implement...

var options = {
  onSwitchChange: function(event, state) {
    // Return false to prevent the toggle from switching.
    return false;
  }
};

$('[name="my-checkbox"]').bootstrapSwitch(options);

Then when you want to programmatically toggle the switch:

$('[name="my-checkbox"]').bootstrapSwitch('toggleState', true);

The true flag will cause the library to skip the onSwitchChange event handler defined above.

Hope that helps.

@nostalgiaz
Copy link
Collaborator

Thanks @mgoldsborough for these lines of documentation ;) I need to talk with @LostCrew in order to decide when and how to release this feature.

@nostalgiaz
Copy link
Collaborator

With @LostCrew , we decided to refactor a little bit the code, anyway this will be released in the 3.3.3 version.

@nostalgiaz nostalgiaz modified the milestones: 3.4.0, 4.0.0 May 25, 2015
@nostalgiaz nostalgiaz modified the milestones: 4.0.0, 3.4.0 May 25, 2015
@fuvizzo
Copy link

fuvizzo commented Jun 8, 2015

@mgoldsborough I got the release 3.3.2.
I put return false; in the onSwitchChange but this does not prevent the switch to toggle.
Did I miss something?

@mgoldsborough
Copy link

@fuvizzo this functionality is not yet released.

@nostalgiaz @LostCrew, any updates on when v3.3.3 (or 3.4.0) will be tagged and released?

@nostalgiaz
Copy link
Collaborator

@mgoldsborough is it ready for production?

@Neelu-Chaturvedi
Copy link

Badly need this functionality. When would it be released

@mgoldsborough
Copy link

@nostalgiaz Yes, I think so? It's working for me. I'd reco a tag/release. Thanks.

@OakBehringer
Copy link

+1 need this released

@nostalgiaz
Copy link
Collaborator

@OakBehringer here we go... the code is in master.

@OakBehringer
Copy link

Awesome - thanks!

@OakBehringer
Copy link

I take it back.

bootstrap-switch.js and bootstrap-switch.min.js haven't been changed for 2 months. Same with the coffee scripts.

???

@santhosh-v
Copy link

+1

3 similar comments
@davidworkman9
Copy link

+1

@shobeurself
Copy link

+1

@crowbarsolutions
Copy link

+1

@asane06
Copy link

asane06 commented May 1, 2016

+1

@badcom
Copy link

badcom commented May 19, 2016

Hasn't this been implemented yet?

@ghost
Copy link

ghost commented Oct 18, 2016

Bump

Has this been implemented yet? Sample code from @mgoldsborough over a year ago doesn't seem to work.

My problem is I have a read-only page with labels and the switch is disabled but users can click on the label to toggle the switch. Check this jsfiddle.

As a side note, if you add the disabled attribute to the checkbox then the functionality works as expected. To me it makes sense to add the disabled attribute to the elements based on the selector when the disabled option is set and remove it when disabled: false. This might be a nonissue, though. Anyone have thoughts on this?

Edit: Stupid me...pending label is set.

@badcom
Copy link

badcom commented Oct 18, 2016

In my case I added a transparent DIV over the switch button, then I change the switch state on a click event. This disabled the slide functionality, but it solved my problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests