Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  93] [ 1]  / answers: 1 / hits: 40154  / 15 Years ago, tue, august 4, 2009, 12:00:00

I am starting a project with jQuery.



What pitfalls/errors/misconceptions/abuses/misuses did you have in your jQuery project?


More From » jquery

 Answers
40

Being unaware of the performance hit and overusing selectors instead of assigning them to local variables. For example:-



$('#button').click(function() {
$('#label').method();
$('#label').method2();
$('#label').css('background-color', 'red');
});


Rather than:-



$('#button').click(function() {
var $label = $('#label');
$label.method();
$label.method2();
$label.css('background-color', 'red');
});


Or even better with chaining:-



$('#button').click(function() {
$(#label).method().method2().css(background-color, red);
});


I found this the enlightening moment when I realized how the call stacks work.



Edit: incorporated suggestions in comments.


[#98991] Friday, July 31, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marint

Total Points: 550
Total Questions: 105
Total Answers: 124

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;