Engineer at AddThis (We are hiring) Open Source Enthusiast Soon-to-be Author Hates the word HTML5 CONTACT DETAILS Github: https://github.com/gfranko Twitter: https://twitter.com/GregFranko LinkedIn: http://linkedin.com/in/gregfranko Stackoverflow: http://stackoverflow.com/users/1172750/greg-franko
THIS: or use a shorter version... $("document").ready(function() { // The DOM is ready! // The rest of the code goes here }); $(function() { // The DOM is ready! // The rest of the code goes here });
IIFE - Immediately Invoked Function Expression (function($, window, document) { // The $ is now locally scoped // Listen for the jQuery ready event on the document $(function() { // The DOM is ready! }); // The rest of the code goes here! }(window.jQuery, window, document)); // The global jQuery object is passed as a parameter jsbin example
IIFE - Immediately Invoked Function Expression (function(yourcode) { // The global jQuery object is passed as a parameter yourcode(window.jQuery, window, document); }(function($, window, document) { // The $ is now locally scoped // Listen for the jQuery ready event on the document $(function() { // The DOM is ready! }); // The rest of the code goes here! } })); jsbin example
// Set's an element's title attribute using it's current text $(".container input#elem").attr("title", $(".container input#elem").text( )); // Set's an element's text color to red $(".container input#elem").css("color", "red"); // Makes the element fade out $(".container input#elem").fadeOut();
YOUR SELECTORS // Set's an element's title attribute using it's current text $("#elem").attr("title", $("#elem").text()); // Set's an element's text color to red $("#elem").css("color", "red"); // Makes the element fade out $("#elem").fadeOut();
YOUR SELECTORS IN VARIABLES // Stores the live DOM element inside of a variable var elem = $("#elem"); // Set's an element's title attribute using it's current text elem.attr("title", elem.text()); // Set's an element's text color to red elem.css("color", "red"); // Makes the element fade out elem.fadeOut();
version... CHAINING // Stores the live DOM element inside of a variable var elem = $("#elem"); // Chaining elem.attr("title", elem.text()).css("color", "red").fadeOut();
// Dynamically building an unordered list from an array var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"], list = $("ul.people"); $.each(localArr, function(index, value) { list.append("<li id=" + index + ">" + value + "</li>"); });
ONCE // Dynamically building an unordered list from an array var localArr = ["Greg", "Peter", "Kyle", "Danny", "Mark"], list = $("ul.people"), dynamicItems = ""; $.each(localArr, function(index, value) { dynamicItems += "<li id=" + index + ">" + value + "</li>"; }); list.append(dynamicItems);
PROMISES, VERY FLEXIBLE function getName(personid) { var dynamicData = {}; dynamicData["id"] = personID; return $.ajax({ url: "getName.php", type: "get", data: dynamicData }); } getName("2342342").done(function(data) { // Updates the UI based the ajax result $(".person-name").text(data.name); });