Share Coding

Tutorials, Problems, Stuffs …

How to improve your jQuery

1. Use unique ‘id’ when selecting (Fastest)

$('#btnTest'); //Correct
$('input#btnDiv'); //Wrong
$('#btnDiv #btnTest'); //Wrong

 
2. Use ‘tag’ before select ‘class’

$('input.setOfButton');

 
3. One select to do all works

// Correct
$('#btnTest')
	.val('Test')
	.css('color', 'red');
	.show();

// Wrong
$('#btnTest').val('Test');
$('#btnTest').css('color', 'red');
$('#btnTest').show();

 
4. Reduce append times

// Correct
var appendHTML = '';
for(var i=0; i<10; i++) {
	appendHTML += '<p>' + i + '</p>';
}
$('#divTest').append(appendHTML);

// Wrong
var $divTest = $('#divTest');
for(var i=0; i<10; i++) {
	$divTest.append('<p>' + i + '</p>');
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: