Manipulating Dom Using Jquery
Manipulating Dom Using Jquery
DOM USING
JQUERY
DOM TREE
2
DOM TRAVERSAL
parent()
• Direct parent element of the
selected element
$(‘title’).parent();
parents()
• All ancestor elements of the
selected element
$(‘p’).parents();
children()
• All direct children of the selected
element
$(‘body’).children();
siblings()
• All sibling elements of the selected
element
$(‘ul’).siblings();
next() / nextAll()
• Next or all next sibling element/s
$(‘p’).next();
prev() / prevAll()
• Previous or all previous sibling
element/s
$(‘ul’).prev();
eq()
• Used to select a specific element
from multiple selected elements
• Uses index number similar to an
array
$(‘.heading’).eq(2);
6
remove()
• Delete specific elements from
selected elements
$(‘.heading’).eq(0).remove();
empty()
• Used to empty out contents of the
selected element
$(‘p’).empty();
7
MANIPULATING
DOM USING
JQUERY
COMMON EVENT HANDLER FUNCTIONS
click()
• occurs when an element is
clicked.
$(‘button’).click(function()
{ //code
});
dblclick()
• occurs when an element is
double-clicked.
$(‘div’).dblclick(function()
{ //code
});
9
COMMON EVENT HANDLER FUNCTIONS
mouseenter()
• occurs when the mouse pointer is
enters the selected element’s area
$(“button").mouseenter(function(
){$(“button").css("background-
color", “green");});
mouseleave()
• occurs when the mouse pointer
leaves the selected element.
$(“button").mouseleave(function(
){$(“button").css("background-
color", "gray");});
10
COMMON EVENT HANDLER FUNCTIONS
mouseover()
• occurs when the mouse pointer
is over the selected element.
$(“button").mouseover(function()
{ //code
});
mouseout()
• occurs when the mouse pointer
is outside the selected element.
$(“button").mouseout(function(){
//code
});
11
COMMON EVENT HANDLER FUNCTIONS
keydown()
• occurs when a keyboard key is
pressed down.
$("input").keydown(function(){
$("input").css("background-
color", "yellow");});
keyup()
• occurs when a keyboard key is
released.
$("input").keyup(function(){
$("input").css("background-
color", “red");});
12
COMMON EVENT HANDLER FUNCTIONS
submit() focus()
• occurs when a form is • occurs when an element gets
submitted. focus.
$("input").submit(function(){ $(“input”).focus(function()
//code { //code
}); });
change() blur()
• occurs when the value of an • occurs when an element loses
element has been changed. focus.
$("input").change(function(){ $(“input”).blur(function(){
//code //code
}); }); 13
COMMON EVENT HANDLER FUNCTIONS
ready() scroll()
• occurs when the DOM has • occurs when the user scrolls in
been loaded. the specified element.
$(document).ready(function( $(“div”).scroll(function(){
){ //code //code
}); });
resize()
• occurs when the browser
window changes size.
$(window).resize(function()
{ //code
}); 14
on() off()
• An alternative programming • It is the opposite method for
technique that can handle the on() method.
events in jQuery • It is used to remove any event
• It replaces any given event handlers for a selected
listening method with itself. element.
• The event listening
method is written as the $("button").click(function(){
first parameter, while the $("p").off("click");
function becomes the });
second parameter.
$(“p”).on(“click”, function(){
$(this).css("background-color",
"pink");
});
15
THE EVENT OBJECT
• It allows event handling functions to receive objects that contain properties and
methods related to their events.
• Event objects are passed to event handler functions as an argument. This allows for the
event object (including its properties and methods) to be used within the event
handling function.
18
toggle()
hide()
• Can be used to alternate
• Used to hide selected
between showing and hiding
element/s
elements
$("button").click(function(){
$("p").toggle(
$("p").hide();
}); function(){$("p").css({"color":
"red"});},
show()
function(){$("p").css({"color":
• Used to show selected
element/s "blue"});},
$("button").click(function(){ function(){$("p").css({"color":
$("p").show(); "green"});
});
});
19
20
fadeIn() fadeToggle()
• Gradually fades an element • Can be used to alternate between
until it is visible. fading in and fading out elements.
$(‘p').click(function(){
$(‘p').click(function(){
$('img').fadeToggle();
$('img').fadeIn();
});
});
fadeOut() fadeTo()
• Gradually fades an element • Used to fade an element to a
until it is invisible. given opacity
• It uses floating-point values from
$(‘p').click(function(){ zero (0) and one (1) to denote
$('img').fadeOut(); opacity percentage
});
$(‘p').click(function(){
$('img').fadeTo(.5);
});
21
22
slideUp()
• Produces an upwards sliding
effect to hide the selected
slideToggle()
element.
• produces an sliding effects to
$(‘p').click(function(){ hide and show the selected
$('img').slideUp();}); element.
$(‘p').click(function(){
slideDown() $('img').slideToggle();
• produces an upwards sliding });
effect to show the selected
element.
$(‘p').click(function(){
$('img').slideDown();});
23
24
animate()
$(document).ready(function(){
$("#btn1").click(function(){
$("#box").animate({height: “+=100px“,
width: “+=100px”}, 1000);}); });
25
Rules for the animate() method
• Any CSS property can be animated using the method, however, ALL property
names must be camel-cased when used with the animate() method.
camelCase is the practice of writing compound words or phrases such
that each word or abbreviation begins with a capital letter with the first
word in lowercase.
• Dashes are eliminated from property names
Example:
CSS properties camelCase
text-transform textTransform
border-radius borderRadius
padding-left paddingLeft
26
Animation Queue
• If multiple animate() method calls are declared one after another, jQuery
creates an "internal" queue for these method calls, then it runs the animate
calls one-by-one in sequence.
$(document).ready(function(){
$("button").click(function(){
var div = $("div");
div.animate({height: ‘600px’, width: ‘300px'}, “fast");
div.animate({width: ‘300px', opacity: ‘600px'}, " fast");
div.animate({height: ‘300px', opacity: ‘600px'}, " fast");
div.animate({width: ‘300px', opacity: ‘300px'}, " fast");
});
});
27