I would like to call from within my plugin an external function and I wish the external function could read the value date-id of the button that was invoked.
plugin.js
;(function ($, window, document, undefined) {
// Function-level strict mode syntax
'use strict';
$.fn.Plugin = function(options) {
var options = $.extend({}, $.fn.Plugin.defaults, options);
return this.each(function() {
var $this = $(this);
$.each(options.commands, function(el, fc) {
$this.on('click', '.' + el, function(e) {
//options.callback.call(fc);
});
});
});
};
$.fn.Plugin.defaults = {
commands: {
add: "addFunc",
edit: "editFunc",
del: "deleteFunc",
show: "showFunc"
},
callback: function() {}
};
})(jQuery, window, document);
index.php
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="js/plugin.js"></script>
<script>
$(function() {
$('#grid').Plugin();
});
function editFunc() {
alert($(this).attr('data-id'));
}
</script>
</head>
<html>
<body>
dinamic table
<table>
<tr>
<td>001</td>
<td>
<button type="button" class="btn btn-warning btn-xs edit" data-id="001"><span class="glyphicon glyphicon-edit"></span></button>
<button type="button" class="btn btn-danger btn-xs delete" data-id="001"><span class="glyphicon glyphicon-trash"></span></button>
</td>
</tr>
<tr>
<td>002</td>
<td>
<button type="button" class="btn btn-warning btn-xs edit" data-id="002"><span class="glyphicon glyphicon-edit"></span></button>
<button type="button" class="btn btn-danger btn-xs delete" data-id="002"><span class="glyphicon glyphicon-trash"></span></button>
</td>
</tr>
...
</table>
How could I do that? Thank you