How to add catch block to jquery plugin
I have a jQuery plugin. I want to add a try catch block to handle exceptions in my jQuery plugin.
My plugin
$(document).ready(function(){
$('.requiredclass').keyup(function() {
$(this).pluginMethod();
});
});
(function($) { //i Want try catch block for this part
// jQuery plugin definition
$.fn.pluginMethod = function(e) {
return this.each(function() {
var $this = $.this;
var som= $this.val();
//My Code goes here
});
};
})(jQuery);
Now if I want to add a try catch block, what would the plugin look like? In case of jquery function We do something like this
function
function myFunction() {
//varible declarations
try {
//Code goes here
}
catch(err) { //We can also throw from try block and catch it here
alert("err");
}
finally {
//code for finally block
}
}
This is now the format we know in the case of a function. But what is the format if I want to add exception handling to the plug-in? In the plugin, from (function($) {
the plugin starts, then there is $.fn.pluginMethod = function(e) {
, followed by
return this.each(function() {`. So where to start the try block and stop it,where to put catch block.Can any one suggest me the format.
If anyone has doubts about understanding the question, please let me know, I will try to explain in more detail.
source to share
I don't think I really have your question. You should be clearer.
But is this what you want?
$(document).ready(function(){
$('.requiredclass').keyup(function() {
$(this).pluginMethod();
});
});
try {
(function($) {
//jQuery plugin definition
$.fn.pluginMethod = function(e) {
return this.each(function() {
var $this = $.this;
var som= $this.val();
//your Code goes here
});
};
})(jQuery);
} catch(err) { //We can also throw from try block and catch it here
alert("err");
} finally {
//code for finally block
}
source to share