Skip to content

Instantly share code, notes, and snippets.

@roden0
Last active December 17, 2015 18:59
Show Gist options
  • Save roden0/5657121 to your computer and use it in GitHub Desktop.
Save roden0/5657121 to your computer and use it in GitHub Desktop.
Jquey Best practices.
// Bad
$("a").click(function(e) {
$(this).addClass("active");
$(this).parent().siblings().find("a").removeClass("active");
var o = $(this).offset();
});
// Better
$("a").click(function(e) {
var $t = $(this),
o = $t.offset();
$t.addClass("active");
$t.parent().siblings().find("a").removeClass("active");
});
$("a").click(function(e) {
// Bad
var id = $(this).attr("id");
// Good
var id = this.id;
});
var catApp = {
$cats: $(".cat"),
init: function() {
this.$cats.click( catClicked )
},
catClicked: function(e) {
alert("'Meow', said " + $(e.target).data("name") );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment