Professional Documents
Culture Documents
Lecture 5 FS
Lecture 5 FS
(IT432)
Anju Mishra
Department of Information Technology
1
Module II- Introduction to jQuery
2
Outline
jQuery Methods
DOM Manipulation
Attribute Selection
Dimension Manipulation
Traversing Elements
CSS Manipulation
Q & A
3
DOM (Document Object Model)
The DOM defines a standard for accessing documents:
4
HTML DOM (Document Object Model)
The HTML DOM is a standard object
model and programming interface for
HTML. It defines:
6
Category Description Imp Methods
Attributes These methods get and set DOM attr(), html(), removeAttr(), prop(),
attributes of elements. val(),
Events These methods are used to handle bind(), blur(), change(), click(), dblclick(),
DOM or JavaScript events. focus(), keyup(), keydown(),
Effects These methods are used to add animate(), fadeIn(), fadeOut(), hide(),
animation to elements. show(),
stop(),
more..
7
Category Description Imp Methods
Forms These methods and event handlers handle forms and their blur(),
various elements. change(),
val(),
submit()
8
Category Description Imp Methods
Data These methods allow us to associate arbitrary data with data(), removeData(),
specific DOM elements. queue(), dequeue(),
clearQueue(),
Miscellaneous These methods are useful in various tasks e.g. traversing each(), index(),
elements, converting to array etc. get(), toArray(),
9
Example
<script type="">
$(document).ready(function () {
});
</script>
10
DOM Manipulation Methods in jQuery
• jQuery provides various methods to add, edit or delete DOM element(s) in the HTML page
Method Description
append() Inserts content to the end of element(s) which is specified by a selector.
before() Inserts content (new or existing DOM elements) before an element(s) which is specified
by a selector.
after() Inserts content (new or existing DOM elements) after an element(s) which is specified by
a selector.
prepend() Insert content at the beginning of an element(s) specified by a selector.
11
DOM Manipulation Methods in jQuery
12
DOM Methods
jQuery after()
$('selector expression').after('content');
• The jQuery after() method inserts content
(new or existing DOM elements) after
target element(s) which is specified by a
selector:
$('selector expression’).before('content');
jQuery before() Method
• The jQuery before() method inserts
content (new or existing DOM
elements) before target element(s)
which is specified by a selector $('selector expression’).append('content');
jQuery append() Method
• The jQuery append() method inserts
content to the end of target element(s)
which is specified by a selector 13
DOM Methods
jQuery prepend()
$('selector
• The jQuery prepend() method inserts
content at the beginning of an element(s) expression’).prepend('content');
specified by a selector:
jQuery before() Method $('selector expression').remove();
• The jQuery remove() method removes
element(s) as specified by a selector.
$('content string').replaceAll('selector
jQuery replaceAll() Method
expression');
• The jQuery replaceAll() method replaces
all target elements with specified
element(s) $('selector expression').wrap('content
jQuery wrap() Method string');
• Specify a selector to get target elements
and then call wrap method and pass
content string to wrap the target element(s) 14
Manipulation HTML Attributes
• The following table lists jQuery methods to get or set value of attribute, property,
text or html.
attr() Get or set the value of specified attribute of the target $('selector
element(s). expression').attr('name','value');
prop() Get or set the value of specified property of the target $('selector
element(s). expression').prop('name','value');
html() Get or set html content to the specified target $('selector
element(s). expression').html('content');
text() Get or set text for the specified target element(s). $('selector
expression').text('content');
val() Get or set value property of the specified target $('selector expression').val('value');
element.
15
Manipulation Attributes
16
Manipulate DOM Element's Dimensions using jQuery
• The jQuery library includes various methods to manipulate DOM element's dimensions like height,
width, offset, position etc.
• The following table lists all the jQuery methods to get or set DOM element's dimensions
18
Manipulate Traversing Elements
• The jQuery library includes various methods to traverse DOM elements in a DOM hierarchy.
• The following table lists all the jQuery methods to get or set DOM element's dimensions
children() Get all the child elements of the specified element(s) $('selector expression').children();
each() Iterate over specified elements and execute specified call back $('selector
function for each element. expression').each(callback function);
find() Get all the specified child elements of each specified element(s). $('selector
expression').find('selector
expression to find child elements');
first() Get the first occurrence of the specified element.
next() Get the immediately following sibling of the specified element. $('selector expression').next();
parent() Get the parent of the specified element(s). $('selector expression').parent();
prev() Get the immediately preceding sibling of the specified element.
siblings() Get the siblings of each specified element(s) $('selector expression').siblings();
19
Manipulate Traversing Elements
20
CSS Manipulation using jQuery
• The jQuery library includes various methods to manipulate style properties and CSS class of DOM element(s).
• The following table lists jQuery methods for styling and css manipulation
css() Get or set style properties to the specified element(s). $('selector expression').css('style
property name','value');
addClass() Add one or more class to the specified element(s). $('selector
expression').addClass('css class
name');
hasClass() Determine whether any of the specified elements are assigned
the given CSS class.
removeClass() Remove a single class, multiple classes, or all classes from the
specified element(s).
toggleClass() Toggles between adding/removing classes to the specified $('selector
elements expression').toggleClass('css class
name');
21
CSS Manipulation using jQuery
22
Difference between position() and offset() in jQuery
• Both the jQueryUI method the returns the object which contains integer co-ordinate
properties of the top and left positions.
• Both functions are applied only on visible elements, not on hidden elements.
23
Difference between position() and offset() in jQuery
The offset() method in jQuery returns the first found position The position() method in jQuery returns the current position
of HTML element with respect to the document. of HTML element with respect to its offset parent.
The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element.
When you want to position a new element on top of another When you want to position a new element near another
one which is already existing, its better to use the jQuery DOM element within the same container, its better to use
offset() method. jQuery position() method.
The offset() method is mostly used in drag-and-drop The position() method is used to position an element relative
functions. to document, window or other elements like mouse or
cursor.
24
Difference between position() and offset() in jQuery
25
Q&A
• Define HTML DOM?
26
Learning Outcome
At the end of this session, you will be able to
Understand the basics of jQuery DOM Techniques
Understand different manipulation methods.
27