IT_211_jQuery_Reviewer
IT_211_jQuery_Reviewer
What is jQuery?
- A fast, small, and feature-rich JavaScript library.
- Simplifies HTML document traversal, manipulation, event handling, animation, and Ajax.
jQuery Syntax:
- Always starts with $ or jQuery.
$(selector).action();
Selectors:
- Element: $("div")
- Class: $(".btn")
- ID: $("#submit")
- Attribute: $("[type='text']")
- Pseudo-Class: $("a:hover")
- Group: $("div, p")
- Descendant: $("form .input")
- this: Refers to the element triggering the event
Events:
- Form: submit, change, focus, blur
- Keyboard: keyup, keydown, keypress
- Mouse: click, dblclick, hover, mouseenter, mouseleave
- Always use $(document).ready() to ensure DOM is loaded.
jQuery Effects
Basic Effects:
- hide(), show(), toggle()
Fade Effects:
- fadeIn(), fadeOut(), fadeTo(), fadeToggle()
Slide Effects:
- slideDown(), slideUp(), slideToggle()
Callback Function:
- Runs after effect is complete:
$("#box").hide(2000, function() {
$(this).show(2000);
});
HTML Manipulation:
- html(): Get/set HTML content
- val(): Get/set form input values
CSS Manipulation:
- css(): Change CSS
$("#box").css({
backgroundColor: "skyblue",
width: "200px",
height: "200px"
});
jQuery Animation
animate() Method:
- Alters CSS properties with animation:
$("#box").animate({
width: "300px",
height: "300px"
}, 500);
Method Chaining:
- $("#box").css("background", "red").slideUp(200).slideDown(200);
jQuery Ajax
Basic Syntax:
$.ajax({
url: "data.php",
}).done(function(response) {
$("#result").html(response);
});
Use Cases:
- Data Retrieval: Load from XML or PHP.
- Data Creation: Submit form data without page reload.