Greg Franko
$("document").ready(function() {
// The DOM is ready!
// The rest of the code goes here
});
or use a shorter version...
$(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();
// 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();
// 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();
or use a shorter version...
// 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>");
});
// 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);
$("#longlist li").on("mouseenter", function() {
$(this).text("Click me!");
});
$("#longlist li").on("click", function() {
$(this).text("Why did you click me?!");
});
var listItems = $("#longlist li");
listItems.on({
"mouseenter": function() {
$(this).text("Click me!");
},
"click": function() {
$(this).text("Why did you click me?!");
}
});
var list = $("#longlist");
list.on("mouseenter", "li", function(){
$(this).text("Click me!");
});
list.on("click", "li", function() {
$(this).text("Why did you click me?!");
});
function getName(personid) {
var dynamicData = {};
dynamicData["id"] = personID;
$.ajax({
url: "getName.php",
type: "get",
data: dynamicData,
success: function(data) {
// Updates the UI based the ajax result
$(".person-name").text(data.name);
}
});
}
getName("2342342");
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);
});