Print Shortlink

jQuery Plugin – III

“fn” property in $ is used to attach new functions in jQuery. You can also develop plugins by using global functions. You can directly assign a new method to $ function as given below:
$.xxx = function(){


Say you want to capitalize a String you can write
$.capitalize = function(value){
 return value.toUpperCase();
}
You can use it by calling alert($.capitalize(“plugin”));
One useful method that I have always written,  is  while working with dates.
$.daysFromNow = function(days){
  var date = new Date();
  date.setDate(date.getDate()+days);
  return date;
}
You invoke it as $.daysFromNow(7) it will return the next week’s date. 
I found the “daysFromNow” example from Venkat Subramaniam‘s  Programming Groovy book. He has implemented a “daysFromNow” function in Groovy using it’s meta-programming capability.
You can also implement a lot of functionalities that involve arrays, in this fashion. 
So crack some code and have fun!!!

Leave a Reply