JavaScript · jQuery · Programming

A very simple jQuery plugin

A very simple plugin that changes the background color of an element on click. Plugin Code This is saved in a js file called myPlugin.js (function($) { //default options var defOption = {“bg”:”#f00″}; var finalOpt = {}; function makeBgRed(obj) { =; } $.fn.myPlugin = function(option) { //util functions finalOpt = $.extend(defOption,option); //here ‘this’… Continue reading A very simple jQuery plugin

JavaScript · Programming

A sample jQuery plugin

An example of a simple jQuery plugin that does some basic form validation. The HTML The HTML is a simple one with just three form fields. <form id="prod_form" method="get" action="#"> <label for="prod-name">Product Name* :</label> <input type="text" name="prod-name" id="prod-name"/> <span class="error" id="prod-name-error"></span><br/> <br/> <label for="prod-quant">Product Quant* :</label> <input type="text" name="prod-quant" id="prod-quant"/> <span class="error" id="prod-quant-error"></span> <br/> <label… Continue reading A sample jQuery plugin

JavaScript · OOP · Programming

Understanding Self Executing Anonymous Functions

Anonymous function is one which has no reference name. For example: function (){}; Creating Self Executing Function Now if we want to make it self-executing then use the following syntax: 1) Wrap the anonymous function within a round brackets. ( function() {…} ) 2) Execute the function by adding the () in the end. (… Continue reading Understanding Self Executing Anonymous Functions

design patterns · JavaScript · Programming

Design Patterns in JavaScript

Singleton Pattern //This is a simple singleton with all public members var simpleSingleton = { “func1” : function() {alert(‘func1’);}, “msg” : “hello” }; //This is complex singleton with private and public members var complexSingleton = (function() { var privateVar = ‘private var’; function privateMethod1(){alert(‘privateMethod1’);} return { publicMethod1 : function(){ //from here all public and private… Continue reading Design Patterns in JavaScript