Marius Djerv
Marius Djerv

Reputation: 265

jQuery passing parameters to plugin

I'm working on my first jQuery plugin, and have some troubles with passing 0 as a parameter.

What I want to do is to set element.eq(parameter) and this works just fine with 1, 2, 3, 4 and so on, but 0 won't work. It works if I type in element.eq(0).

My console.log outputs:

0
null x2

I have a simple jsfiddle with the problem

So how would I go about to make it show first child?

$.fn.miobi.defaultOptions = {
        page: null,
        pageActive: null
    };

$('body').miobi({
    page: '.page',
    pageActive: 1 // not working with 0
});

Upvotes: 1

Views: 47

Answers (2)

Phatjam98
Phatjam98

Reputation: 312

Here ya go

http://jsfiddle.net/Phatjam98/gqg8qnqm/

So your if is throwing false because 0 is falsey in JS. So change:

if(options.pageActive){

To:

if(options.pageActive !== undefined){

Upvotes: 1

David Thomas
David Thomas

Reputation: 253308

The problem is that 0 is a falsey value, so rather than testing if it's truthy (it isn't), explicitly check for it being not-undefined:

if (typeof options.pageActive !== 'undefined') {

JS Fiddle demo.

Upvotes: 4

Related Questions