Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] First time creating a plugin, is this proper? Re: [Solved] First time creating a plugin, is this proper?

#104735
SgtLegend
Member

When you create a plugin you always need to return the current jQuery object for the element which allows for method chaining, also rather then using the keydown() and keyup() methods you should be using the on() method as it has event delegation built into it which allows for more dynamic event handlers in the DOM. See the below code which i updated to reflect this.

(function($) {
$.fn.codeScanner = function() {

return this.each(function() {
var scanID = '';

this.on({
keydown :, function(e) {
scanID += 'ok';
},
keyup : function(e) {
alert(scanID);
}
});
});

};
})(jQuery);