0

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 :)

3
  • Right, so what's the problem? selected shouldn't be in quotes btw. Commented Nov 28, 2011 at 14:54
  • @RichardD The problem is, the function is only supposed to execute when mouseDown = 1, but it executes on mousemove Commented Nov 28, 2011 at 15:03
  • You should probably have mentioned that in your question ;) Commented Nov 28, 2011 at 15:11

4 Answers 4

1

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/

Sign up to request clarification or add additional context in comments.

6 Comments

the problem is, the function is only supposed to execute on when mouseDown = 1, but it executes on mousemove
It's because you've added handler directly to mousemove: }).mousemove(options.selected).click(function() {. Try this one: jsfiddle.net/Neftr/9
And this one fires a little bit less events: jsfiddle.net/Neftr/11. But you still need to debug it.
getting there :) but what if I want the function to something like this $(this).text('selected') so the text of the selected table cell will change. Is this possible?
Yes, just pass as a param this when you calling function, and you will be able to get element on which onmousemove event triggered.
|
0

change you options to

selected: function() {

}

so when calling the plugin you can do

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

Comments

0

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

Just use options.selected

2 Comments

the problem is, the function is only supposed to execute on when mouseDown = 1, but it executes on mousemove
yes, but it's also inside the if(mouseDown === 1) statement
0

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.
    }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.