Skip Navigation Links



Easy Tip to write clean and manageable jQuery Code

Author : Virendra Dugar      Blog :jQuery By Example      Date: 7/6/2012 4:30:00 AM


jQuery gives amazing power to make things simple and easy to implement. For jQuery to work, we need to put all the code in $(document).ready() function and there can be many $(document).ready() function in a single page.

But putting everything inside $(document).ready() function may make your code look ugly, not understandable, unmanageable and sometimes buggy. In this post, I will show you how can you make your code look clean, easy to understand and manageable.


We all are familiar with below jQuery code. Most of us have habit of putting everything inside $(document).ready. Below code is very small piece of jQuery code but in real projects or application this would be more and then things become unmanageable. So is there any better way to do this?
$(document).ready(function() {
  $("a").live('click', function() {
      alert('I am clicked');
  });
  $("a").mouseover(function() {
      $(this).css('color','red');
  });
});
Well, the answer is YES. You can divide your code in functions.
//This function binds the click event to all the a element using live method.
function BindLivetoAnchor()
{
  $("a").live('click', function() {
      alert('I am clicked');
  });
}

//This function binds the mouseover event to all the a element.
function BindMouseOverEvent()
{
  $("a").mouseover(function() {
      $(this).css('color','red');
  });
}
Now these functions can be called from document.ready() function.
$(document).ready(function() {
   BindLivetoAnchor();
   BindMouseOverEvent();
});
This way you can make your jQuery code look
  • Clean and manageable
  • Easy to understand.
  • Commenting can be done easily.
  • Easy to debug.
  • It can be reused.

Feel free to contact me for any help related to jQuery. I will gladly help you.

Bloggers

active bloggers in the last 24 hrs. Number shown in the bracket represents number of posts published in past 24 hrs,


other authors(79)

Fidarose Isha(5)

abrachan(2)

Dream Killers 11(2)

Milind(2)

N.GURURAJ.(2)

Prasanta Bora(2)

Prof.V.Harihara Subramanian(2)

R.D. Bhalekar(2)

Vidya lakshmi(2)

--- :) ----(1)

...yliram...(1)

A.M(1)

Abraham Tharakan(1)

aburman(1)

Aditya Iyer(1)

Alice(1)

Ambica Srimal(1)

Ambika(1)

Anu Varma(1)

Arti(1)

Artnavy(1)

arunimamazumdar(1)

cartoonistpankaj(1)

churumuri(1)

Cynical(1)

Cynthia Z(1)

dawdayogesh(1)

Defiant Princess(1)

Devi(1)

Dew(1)

DIGITALASIAN(1)

eM(1)

ephemeral desires(1)

etniq(1)

Farida Rizwan(1)

Flights of Fantasy(1)

For The Love Of Fashion And Other Things(1)

Gauri(1)

Giribala(1)

Hari Chandana P(1)

iBeingMe(1)

Itchingtowrite(1)

Iti(1)

Jaspreet(1)

Juhi From Meerut(1)

Jutson Devasahayam(1)

Kalyani(1)

Krishnaveni(1)

Kulsum(1)

Lakshmi Andra(1)

liveonimpulse(1)

Lucius Lobo(1)

mad madrasi(1)

Mahesh Kalaal(1)

Mahimaa Kohli(1)

Meena(1)

Megha(1)

MUNZ TDT(1)

Mythreyi(1)

Neelam Dadhwal(1)

Nik P(1)

nimisha mistry(1)

P.(1)

parth joshi(1)

Pavan BhavaniShekhar Devarakonda(1)

PNA(1)

Pramod Ganapathi(1)

pravsphenomenon(1)

Preety(1)

Priya(1)

Raafay Awan(1)

Reshma Rohra(1)

revsjoiedevivre(1)

Richa Sharma(1)

Ritesh Agarwal(1)

RollerCoaster(1)

Rupertt Wind(1)

sankarshan(1)

SAPTARSHI BASU(1)

sathish kumar(1)

sathishk(1)

Sharmila(1)

Shazia M(1)

Sheenzie(1)

Shivlu Jain(1)

Shobha(1)

Shruthi(1)

Simply delicious(1)

Simran(1)

Srinivasan Sampathkumar(1)

Sum(1)

sumana Mukhopadhyay(1)

Sumit K. Jha(1)

Supernova(1)

Swaram(1)

Swati Sapna(1)

Tarang Sinha(1)

TeaForTravel(1)

the blogger formerly known as sansmerci(1)

Think Out of the Box Communications Pvt. Ltd.(1)

tikulicious(1)

Tinkoo(1)

Vidya Sury(1)

Vikram Karve(1)

Vinay Kumar Vaidya(1)

Vishal Velekar(1)

vishesh unni raghunathan(1)

wildflower(1)

Yuvika(1)

अली सैयद(1)