Category Archives:

Chris and Corey: JQuery Quick Tips

Posted on March 17, 2008 by Corey Maass

Avoiding Repetitious Code in JQuery

A good tip in any coding language is to not repeat code. If you find yourself using a bit of code more than once, make it a separate function. In JQuery, you have the advantage of expanding the JQuery object to include your code. If you want to have the code process JQuery-selected items, use jQuery.fn.nameOfFunction. Otherwise, use jQuery.nameOfFunction. Finally, if you find yourself extending JQuery a lot, use jQuery.fn.extend and put all your code in one place.

1) JQuery extended function when you want to access a JQuery array of elements: Example use: 2) JQuery extended function: Example use:

| Comments (0) | Technorati Tags :