Topics: tools, development
Author: Maitrik
jQuery is a library, or set of helpful add-ons, to the JavaScript programming language. It may seem counterintuitive to learn how to use a library before learning the actual language, but there are a few good reasons for this.
It takes a while to become comfortable with JavaScript, and it's trickier to manipulate HTML elements directly with JavaScript than with jQuery. In order to help you build awesome websites faster, we're starting you off with jQuery.
jQuery provides a simple interface for the underlying JavaScript. It's easier for many users to learn jQuery first, then dive into the nitty-gritty JavaScript details later.
jQuery is much better at giving you immediate, visual results than regular JavaScript. By the end of this lesson, you'll have built your own interactive button!
$(document).ready(function() {
$('div').action(howfast);
});
var $div= $('div');
$p = $("<p>I'm a new paragraph!</p>");
$(document).ready(function() {
$('div').click(function() {
$(this).fadeOut('slow');
});
});
$(".info").append("<p>Stuff!</p>");
$(".info").prepend("<p>Stuff!</p>");
$('<p>Stuff!</p>').appendTo('.info');
$('<p>Stuff!</p>').pretendTo('.info');
$(document).ready(function(){
var $paragraph = $('<p>text goes here</p>');
$('div#one').after($paragraph);
$('div#two').after($paragraph);
});
var $paragraph = $("p"); // existing element
$("div").after($paragraph); // Move it!
// Same as:
$("div").after($("p"));
$(document).ready(
function(){
$('#one').after("<p>You are my sunshine</p>");
$('#two').after($('p'));
}
);
we have two jQuery functions, .empty() and .remove(), that help us delete content from our pages
.empty() deletes an element's content and all its descendants. For instance, if you .empty() an 'ol', you'll also remove all its 'li's and their text.
.remove(), not only deletes an element's content, but deletes the element itself.
$('selector').remove();
$('selector').empty();
jQuery includes two functions, .addClass() and .removeClass(), that can be used to add or remove a class from an element.
You aren't selecting anything, you are modifying your element. This means that you do not need # or . before your class.
$('selector').addClass('className');
$('selector').removeClass('className');
$('#text').toggleClass()('highlighted');
$("div").height("100px");
$("div").width("50px");
$("div").css("background-color","#008800");
.html() can be used to set the contents of the first element match it finds. For instance, will get the HTML contents of the first div it finds, and will set the contents of the first div it finds to "I love jQuery!" s $('div').html(); $('div').html("I love jQuery!");
.val() is used to get the value of form elements. For example, would get the value of the first checked checkbox that jQuery finds.
$('input:checkbox:checked').val();
The .keydown() event is triggered whenever a key on the keyboard is pressed. It only works on whatever page element has focus, so you'll need to click on the window containing your div before pressing a key in order for you to see its effects.
Let's go ahead and combine our new event with a new effect: .animate()! We'll use this to move an object on the screen whenever we press a key.
The .animate() effect takes two inputs: the animation to perform, and the time in which to perform the animation. Here's an example:
$(document).ready(function() {
$(document).event(function() {
$('div').effect(anim, duration);
});
});
//--Example
$(document).ready(function() {
$('div').animate({left:'+=10px'},500);
});
$(document).ready(function() {
$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
// Left arrow key pressed
case 37:
$('img').animate({left: "-=10px"}, 'fast');
break;
// Up Arrow Pressed
case 38:
$('img').animate({top: "-=10px"},'fast');
break;
// Right Arrow Pressed
case 39:
$('img').animate({left: "+=10px"}, 'fast');
break;
// Down Array Pressed
case 40:
$('img').animate({top: "+=10px"}, 'fast');
break;
}
});
});