Lee Price
Lee Price

Reputation: 5212

function within a jQuery plugin

I have written a plugin that allows a user to select table cells:

https://jsfiddle.net/leeprice/Neftr/

I have the main functionality complete, however, I have a problem. I need to be able to able to pass the selected function as a plugin option. You'll see where I've commented in the fiddle.

The problem is, the function is only supposed to execute when mouseDown = 1, but it executes on mousemove

any help appreciated :)

Upvotes: 0

Views: 76

Answers (4)

Rory McCrossan
Rory McCrossan

Reputation: 337560

I've updated your fiddle here

Pertinent changes:

In the intialisation you pass the anonymous function:

$(function() {
    $('table').cellSelect({ selected: function() { alert("foo"); }});
});

Then when required, run the function assigned to the options.selected parameter:

if (mouseDown === 1) {
    if (shiftDown === 1) {
        $(this).removeClass('selected');
    } else {
        $(this).addClass('selected');
        options.selected; // run passed anonymous function, or default specified one.
    }
}

Upvotes: 0

Naftali
Naftali

Reputation: 146310

Here you go: https://jsfiddle.net/maniator/Neftr/3/

Just use options.selected

Upvotes: 0

Samich
Samich

Reputation: 30115

Use initialization:

$('table').cellSelect({selected: function() { 
                                    alert('selected'); }
                              });

And call your handler:

// Needs to execute here
if (options.selected)
    options.selected();
}

Code: https://jsfiddle.net/Neftr/6/

Upvotes: 1

Manuel van Rijn
Manuel van Rijn

Reputation: 10305

change you options to

selected: function() {

}

so when calling the plugin you can do

$('table').cellSelect({
    selected:function() {
        alert('b');
    }
});

Upvotes: 0

Related Questions