JQuery
JQuery
WHAT IS JQUERY?
WHY JQUERY?
Microsoft
IBM
Netflix
JQUERY SYNTAX
Examples:
JQUERY SELECTORS
The Element Selector
The jQuery element selector selects elements based on the element name.
You can select all <p> elements on a page like this:
$("p")
Example:
When a user clicks on a button, all <p> elements will be hidden:
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
JQUERY SELECTORS
The #id Selector
The jQuery #id selector uses the id attribute of an HTML tag to find the specific
element.
An id should be unique within a page, so you should use the #id selector when
you want to find a single, unique element.
To find an element with a specific id, write a hash character, followed by the id of
the HTML element:
$("#test")
Example:
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
JQUERY SELECTORS
The .class Selector
The jQuery class selector finds elements with a specific class.
To find elements with a specific class, write a period character, followed by the
name of the class:
$(".test")
Example:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
JQUERY SELECTORS
Syntax
Description
$("*")
$(this)
$("p.intro")
$("p:first")
$("ul li:first")
$("ul li:first-child")
$("[href]")
$("a[target='_blank']")
$("a[target!='_blank']")
Selects all <a> elements with a target attribute value NOT equal to
"_blank"
$(":button")
$("tr:even")
$("tr:odd")
JQUERYEVENT METHODS
Here are some common DOM events:
Mouse Events
Keyboard Events
Form Events
Document/Window
Events
click
keypress
submit
load
dblclick
keydown
change
resize
mouseenter
keyup
focus
scroll
mouseleave
blur
unload
$("p").click();
The next step is to define what should happen when the event
fires. You must pass a function to the event:
$("p").click(function(){
// action goes here!!
});
click()
$("p").click(function(){
$(this).hide();
});
dblclick()
$("p").dblclick(function(){
$(this).hide();
});
mouseenter()
The mouseenter() method attaches an event handler function to an
HTML element.
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
mouseleave()
The mouseleave() method attaches an event handler function to an
HTML element.
mousedown()
The mousedown() method attaches an event handler function to an
HTML element.
hover()
The hover() method takes two functions and is a combination of the
mouseenter() and mouseleave() methods.
focus()
The focus() method attaches an event handler function to an HTML form
field.
focus()
The focus() method attaches an event handler function to an HTML form
field.
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
The blur() method attaches an event handler function to an HTML form
field.
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
The on()
The on() method attaches one or more event handlers for the selected
elements.