Skip to content

Instantly share code, notes, and snippets.

@Sljubura
Last active December 13, 2015 20:58
Show Gist options
  • Save Sljubura/4973780 to your computer and use it in GitHub Desktop.
Save Sljubura/4973780 to your computer and use it in GitHub Desktop.
Some jQuery tips.
// Always use lates version
// Not all selectors are equaly fast
// Fastest to slowest selectors.
$('#selector-by-id');
$('div', 'input');
$('.class-selectors');
// And the slowest are pseudo selectors
$(':visible, :hidden, [attribute=value]');
// Efficient selectors
$('#wrapper').find('.first'); // Better
$('#wrapper .content .main article.first');
// Use caching!
var $firstArticle = $('#wrapper').find('.first');
// But avoid using global variables by using immediate function or similar.
// Use chaining
$firstArticle
.fadeIn()
.addClass('active')
.css({ // Also, use object notation for adding multiple css rules
paddingLeft: '20px', // instead of chaining .css() multiple times.
marginLeft: '20px'
});
// Attaching data
$('#node').data(key, value); // Slower, creates jQuery object
$.data('#node', key, value); // Faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment