Biker John
Biker John

Reputation: 2711

Triggering datepicker from function doesnt work

I use a pickadate.js plugin.

What i would like to do is to trigger a date container after checkbox is checked, but it somehow doesn't work. I assume it has to do something with out of the scope variables since it works just fine outside the checkbox event function.

Official documentation:

See here and also here

JS:

var pick = $('#chosen').pickadate({format:'dd.mm.yyyy'});
var picker = pick.pickadate('picker');
//picker.open();
// it works here

$(":checkbox").change(function() {
    if(this.checked) {

        picker.open();
        // it doesnt work here
        event.stopPropagation();

    console.log('checkbox triggers!');

    }
});

JSFIDDLE

Upvotes: 0

Views: 158

Answers (2)

Anujith
Anujith

Reputation: 9370

Use .on() with click. (like (.on("event", fn)))

$(":checkbox").on('click', function () {
    if (this.checked) {
        picker.open();
        event.stopPropagation();
    }
});

Fiddle

Upvotes: 1

jyrkim
jyrkim

Reputation: 2869

I used trigger method and now I think it works as intended:

$( "input[type=checkbox]" ).on( "click", function(){

    $(":checkbox").trigger("change");

} );

Fiddle

Upvotes: 1

Related Questions