BRB BRB - 4 months ago 10
Javascript Question

jQuery plugin accept array as an option

I'm creating my first jQuery plugin.

var defaults = {
breakpoint: {
width: 1920,
target: "",
place: "after"
}
}


Here is my default options. I would like to accept several breakpoints. Something like Slick carousel responsive: option.

$( "a" ).move({
breakpoint: {
width: 1800,
target: "p",
place: "after"
},
breakpoint: {
width: 1500,
target: "p",
place: "after"
}
});


When I try to do this, I get only last one breakpoint information(that one with 1500 width).

How can I correctly declare my defaults array to accept as many arrays of values as user wants?

Answer

if breakpoints is an array it should be formatted as:

var defaults = {
    breakpoints: [
        {
            width: 1920,
            target: "",
            place: "after"
        }
    ]
}

and

I'm creating my first jQuery plugin.

var defaults = { breakpoints: { width: 1920, target: "", place: "after" } } Here is my default options. I would like to accept several breakpoints. Something like Slick carousel responsive: option.

$( "a" ).move({
    breakpoint: [
        {
            width: 1800,
            target: "p",
            place: "after"
        },
        {
            width: 1500,
            target: "p",
            place: "after"
        }
    ]
});
Comments