MacMan MacMan - 2 months ago 8
jQuery Question

JQuery - Access function from within plugin?

I'm trying to call a function from within a jquery plugin.

The example code is:

$("#table-2").tableDnD({
onDragClass: "myDragClass",

onDrop: function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
$(#debugArea).html(debugStr);
},

onDragStart: function(table, row) {
$(#debugArea).html("Started dragging row "+row.id);
}
});


The definition of onDrop taken from
http://isocra.com/2008/02/table-drag-and-drop-jquery-plugin/
is

onDrop

Pass a function that will be called when the row is dropped. The function takes 2 parameters: the table and the row that was dropped. You can work out the new order of the rows by using
table.tBodies[0].rows.

Is it possible to pass a function to this ?
eg :

onDrop: doWork(table, row),


Where doWork is:

function doWork (table, row) {
var row_id = $(row).prop('id');
}


The error is get is:
Uncaught ReferenceError: table is not defined(…)
which relates to the line I added for doDrop.

Anyone know anyway to do this ?
Thanks.

Answer

When you write

onDrop: doWork(table, row),

You are calling the function doWork with those parameters. As table doesn't exists it throws the error.

The correct way to do what you are trying is

onDrop: doWork,

This way you are setting the function you want to call.