RSS

Blog

10 useful jQuery authoring tips

h318 Dec 2008 –  Comments (10)

Comments

Nice list of tips. Some of them were new to me.

For the 5th point, isn't it more effective to save the active element to a global variable and deactivate just that one instead of all siblings?

Regarding the 6th point, isn't it a waste of memory and time to use the jquery-based iterations, when you can do that with the for loop? Especially, when it's all about simple going through each element in an array. What do you win avoiding normal for loops?

[] Archatas ~ 1 year, 8 months ago at 8:11 a.m.

Good list. The only thing I would add, is to change #2, in order to prevent conflicts with $, to:

jQuery(function($){ ... }

[] Eric Martin ~ 1 year, 7 months ago at 3:44 p.m.

I can tell #4 is going to be a tough one to remember for me. Great list, thanks for the post.

[] eric fickes ~ 1 year, 5 months ago at 11:58 a.m.

#5 and #8 are truly amazing. Thanks for making them appear them so easy than they really are(for me, of course).

[] Team Nirvana ~ 1 year, 5 months ago at 3:15 p.m.

Thanks for this.

The #2 is great, I never seen this before.

[] Skoua ~ 1 year, 5 months ago at 7:42 p.m.

whats the point in #7 (namespaces)? when would you use it?

[] ernesto ~ 1 year, 5 months ago at 3:34 p.m.

@ernesto

Suppose two plugins bind a onclick event on a elements. If you don't use namespaces the last plugin to be loaded will override the first plugin's event callback.

Unbinding the click event will also cause the click event to be unbinded for both plugins.

Proper namespacing ensure that no conflict will happens and everything works as expected.

It also allow to bluk unbind events by namespace, which can be pretty useful.

[] h3 ~ 1 year, 5 months ago at 11:02 p.m.

I'm trying to be lazy but it's biting me:

$("#no-such-element).load("myfile");

Even if no match is found, myfile is loaded from the server. Easiest way to see this is with Firebug. Point load to a file that doesn't exist and watch how you get a 404 in the console.

[] Daniel ~ 1 year, 4 months ago at 3:24 a.m.

@Daniel

I also noticed this inconsistent behavior with load. Why they decided to handle this situation like this is beyond my understanding, but you've peaked my curiosity. I will investigate that and maybe fill a bug report.

[] h3 ~ 1 year, 4 months ago at 9:09 a.m.

Thanks very much! This is just helping with making more 'beautiful' and smaller code. I like this smarter approach.

[] Elemental Web Development ~ 1 year, 1 month ago at 4:14 a.m.

Copyrighted stuff .. u know.