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

Changing a Switchery checkbox state from code #25

Closed
ghost opened this issue Mar 12, 2014 · 12 comments
Closed

Changing a Switchery checkbox state from code #25

ghost opened this issue Mar 12, 2014 · 12 comments

Comments

@ghost
Copy link

ghost commented Mar 12, 2014

Not convenient approach for initialization and no why to pass state via setting nor via java-script.

Please provide a way to change state from code

@MojoDK
Copy link

MojoDK commented Mar 17, 2014

+1

@guillerodriguez
Copy link

Can't you just call swtich.setPosition(true) ?

@abpetkov
Copy link
Owner

The above does the trick for you guys. Thanks for the answer @guillerodriguez!

@dafinley
Copy link

Find the element using jquery then remove all siblings. Then you can you just use switchery again.
$('input.js-switch').siblings().remove();
adjust the value (checked/disabled)
new Switchery($('input.js-switch')[0])

@JacerOmri
Copy link

@dafinley idea is the best so far...

@damiensawyer
Copy link

damiensawyer commented May 12, 2016

After playing around with all these, the best way I found was to just send it a click via Jquery.
$(element).trigger('click');

@RomaricMourgues
Copy link

I am agree with damiensawyer, I wrote a little function to setState an element
function changeSwitcheryState(el,value){
if($(el).is(':checked')!=value){
$(el).trigger("click")
}
}

@damiensawyer
Copy link

Cool.

If anyone's interested, I implemented it as a knockout js binding as follows. This allows it to be bound to an observable boolean:

ko.bindingHandlers.switchery = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var options = (allBindingsAccessor().options ||
            {
                size: 'small'
            });
        var v = valueAccessor();
        $(element).prop('checked', v());
        var e = new Switchery(element, options);
        element.onchange = function () {
            if (ko.isObservable(v)) {
                v(element.checked);
            }
        };
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var value = ko.unwrap(valueAccessor());
        var currentValue = element.checked;
        if (currentValue != value) {
            //e.setPosition(value); // didn't seem to work... so just send it a click.
            $(element).trigger('click');
        }
    }
};
//ko.virtualElements.allowedBindings['switchery'] = false;

@lfern
Copy link

lfern commented Jul 3, 2017

Try to change checked property and then invoke setPosition()

@FrankenApps
Copy link

FrankenApps commented Nov 1, 2017

I have two checkboxes and I want the not clicked checkbox to change when I click one of them (essentially a radio group with two radio buttons). Now when I .trigger('click') it seems to somehow override the change of the clicked checkbox, so that only the not clicked changes its state. See this JSFiddle.

@PhilippeMarcMeyer
Copy link

PhilippeMarcMeyer commented Nov 23, 2017

For myself, I check if the checkbox is a different state than the one I want and then trigger a click
var isChecked = $("#myCheckBox").is(':checked');
if (isChecked != data.IsActive) {
$("#myCheckBox").parent().find(".switchery").trigger("click");
}

@LuizFritsch
Copy link

I had this problem and I was calling an onclick function so I couldn't trigger any click event or It would start an infinite loop. I managed to solve like this:

$("#" + registro).siblings('span').remove();
var sw = new Switchery($("#" + registro)[0]);
sw.setPosition(true);

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

No branches or pull requests