Lec #4 Jquery Intro New
Lec #4 Jquery Intro New
Lec #4 Jquery Intro New
jQuery Introduction
The purpose of jQuery is to make it much easier to use JavaScript on your website.
What is jQuery?
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish,
and wraps them into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and
DOM manipulation.
Why jQuery?
There are lots of other JavaScript frameworks out there, but jQuery seems to be the most
popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery, such as:
Google IBM
Microsoft Netflix
jQuery Syntax
1
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Examples:
You might have noticed that all jQuery methods in our examples, are inside a document ready
event:
$(document).ready(function(){
});
This is to prevent any jQuery code from running before the document is finished loading (is
ready).
jQuery Selectors
In this section, you will learn about jQuery selectors and how to find DOM element(s) using
selectors.
The jQuery selector enables you to find DOM elements in your web page. Most of the times you
will start with selector function $() in the jQuery.
Syntax:
$(selector expression, context)
The context parameter is optional. It specifies elements in a DOM hierarchy from where
jQuery starts searching for matching elements.
The most common selector pattern is element name. Specifing an element name as string e.g. $
(‘p’) will return an array of all the <p> elements in a webpage.
The following figure shows which DOM elements will be returned from $('p') & $(‘div').
2
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
As you can see in the above figure, $('div') will return all the <div> elements including
its child elements.
<div>
<p></p>
<p></p>
</div>
<p></p>
<div></div>
The following figure shows which DOM elements will be returned from $('#myDiv1') &
$'(#prg2').
3
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
<div id="myDiv1">
<p></p>
</div>
<p id="impPrg"></p>
<div id="myDiv2">
</div>
In the following example, jQuery returns all the elements that have class or contenteditable
attribute irrespective of any value.
4
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
<div id="myDiv1">
<p></p>
</div>
<div id="myDiv1">
<p></p>
</div>
5
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
jQuery provides number of ways to select a specific DOM element(s). The following table
lists the most important selector patterns.
6
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
7
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
jQuery Methods
You learned about jQuery selectors in the previous section. The jQuery selector finds
particular DOM element(s) and wraps them with jQuery object. For example,
document.getElementById() in the JavaScript will return DOM object whereas $('#id') will
return jQuery object. The following figure illustrates the difference.
jQuery Methods
As you can see in the above figure, document.getElementById function returns div element
whereas jQuery selector returns jQuery object which is a wrapper around div element. So now,
you can call jQuery methods of jQuery object which is returned by jQuery selector.
jQuery provides various methods for different tasks e.g. manipulate DOM, events, ajax etc.
The following table lists different categories of methods.
8
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Dimensions These methods are used to get and set the CSS height(),width(),innerHeight(),
dimensions for the various properties. innerWidth(),more..
9
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
10
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
The following example shows how to use some of the jQuery methods to manipulate DOM
elements.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="~/Scripts/jquery-2.1.3.js"></script>
<script type="">
$(document).ready(function () {
});
</script>
</head>
<body>
<div id="myDiv"></div>
<p></p>
<span></span>
</body>
</html>
jQuery provides various methods to add, edit or delete DOM element(s) in the HTML page.
The following table lists some important methods to add/remove new DOM elements.
Method Description
before() Inserts content (new or existing DOM elements) before an element(s) which is
specified by a selector.
11
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Method Description
after() Inserts content (new or existing DOM elements) after an element(s) which is
specified by a selector.
wrap() Wrap an HTML structure around each element which is specified by selector.
The following figure shows how the DOM manipulation methods add new elements.
Syntax:
$('selector expression').after('content');
First of all, specify a selector to get the reference of target element(s) after which you
want to add the content and then call after() method. Pass the content string as a
parameter. Content string can be any valid HTML element.
12
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
Output:
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
Syntax:
$('selector expression').before('content');
Specify a selector to get the reference of target element(s) before which you want to add
the content and then call before() method. Pass the content string that can be any valid
HTML element as parameter.
<div id="div1">div 1
</div>
<div id="div2">div 2
</div>
Output:
<div style="background-color:yellow"> New div </div>
<div id="div1">div 1
</div>
13
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
<div id="div2">div 2
</div>
Syntax:
$('selector expression').append('content');
First specify a selector expression to get the reference of an element(s) to which you want
to append content, then call append() method and pass content string as a parameter.
$('p').append('World!');
<p>Hello </p>
Output:
<p>Hello World!</p>
Syntax:
$('selector expression').prepend('content');
First specify a selector expression to get the reference of an element(s) to which you want
to prepend the content, then call prepend() method and pass content string as a parameter.
<div>
<label>This is div.</label>
</div>
Output:
<div>
<p>This is prepended paragraph</p>
<label>This is div.</label>
</div>
14
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Syntax:
$('selector expression').remove();
First specify a selector expression to get the reference of an element(s) which you want to
remove from the document and then call remove() method.
15
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
$('label').remove();
<div>This is div.
<label>This is label.</label>
</div>
Output:
<div>
This is div.
</div>
Syntax:
$('content string').replaceAll('selector expression');
Here, syntax is different. First specify a content string as replacement element(s) and then
call replaceAll() method with selector expression to specify a target element(s).
$('<span>This is span</span>').replaceAll('p');
<div>
<p>This is paragraph.</p>
</div>
Output:
<div>
<span>This is span</span>
</div>
<span>This is span</span>
Syntax:
$('selector expression').wrap('content string');
Specify a selector to get target elements and then call wrap method and pass content string
to wrap the target element(s).
16
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
$('span').wrap('<p></p>');
<div>
<span>This is span.</span>
</div>
<span>This is span.</span>
Output:
<div>
<p> <span>This is span.</span></p>
</div>
<p><span>This is span.</span></p>
Manipulate HTML Attributes using jQuery:
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 element(s).
prop() Get or set the value of specified property of the target element(s).
The following figure shows various jQuery methods to access DOM element's attributes,
properties and values.
17
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Syntax:
$('selector expression').attr('name','value');
First of all, specify a selector to get the reference of an element and call attr() method
with attribute name parameter. To set the value of an attribute, pass value parameter along
with name parameter.
<div>
This is div.
</div>
<p style="font-size:16px;font-weight:bold">
This is paragraph.
</p>
<div>
This is div.
</div>
<p>
This is paragraph.
</p>
In the above example, $(‘p’).attr(‘style’) gets style attribute of first <p> element in a
html page. It does not return style attributes of all the <p> elements.
18
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
The jQuery prop() method gets or sets the value of specified property to the DOM element(s).
Syntax :
$('selector expression').prop('name','value');
First of all, specify a selector to get the reference of an element(s) and call prop()
method. Supply "name" parameter to get the value of that property. To set the value of a
property, specify a value parameter along with name parameter.
<div>
This is div.
</div>
<p style="font-size:16px;font-weight:bold">
This is paragraph.
</p>
<div>
This is div.
</div>
<p>
This is paragraph.
</p>
In the above example, $(‘p’).prop(‘style’) returns an object. You can get different style
properties by using object.propertyName convention e.g. style.fontWeight. Do not include '-'
as a property name.
Syntax:
$('selector expression').html('content');
First of all, specify a selector to get the reference of an element and call html() method
without passing any parameter to get the inner html content. To set the html content,
specify html content string as a parameter.
19
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
</p>
</div>
<div id="emptyDiv">
</div>
Syntax:
$('selector expression').text('content');
First of all, specify a selector to get the reference of an element and call text() method
to get the textual content of an element. To set the text content, pass content string as a
parameter.
20
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
//removes all the content from #emptyDiv and inserts "This is some text." to it
$('#emptyDiv').text('This is some text.');
Please note that text() method only returns text content inside the element and not the
innerHtml.
The jQuery val() method gets or sets value property to the specified DOM element(s).
Syntax:
$('selector expression').val('value');
First of all, specify a selector to get the reference of an element and call val() method to
get the value of value property. To set the text content, pass content string as a
parameter.
Example: jQuery val() method
<div>
<label>Name:</label><input type="text" />
</div>
<div>
<input type="Submit" value="Save" />
</div>
In the above example, val() method returns value of "value" property. If element does not
support value property then val() method returns null.
The jQuery library includes various methods to manipulate DOM element's dimensions like
height, width, offset, position etc.
21
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
The following table lists all the jQuery methods to get or set DOM element's dimensions.
jQuery
Method Description
innerHeight( Get or set inner height (padding + element's height) of the specified
) element(s).
outerHeight( Get or set outer height (border + padding + element's height) of the specified
) element(s).
offset() Get or set left and top coordinates of the specified element(s).
innerWidth() Get or set the inner width (padding + element's width) of the specified
element(s).
outerWidth() Get or set outer width (border + padding + element's width) of the specified
element(s).
22
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Syntax:
$('selector expression').height(value);
Specify a selector to get the reference of an element and call height() method to get the
height in pixel. To set the height of specified elements, specify height as integer
parameter in height() method.
<p>
This is paragraph.
</p>
23
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
The jQuery width() method gets or sets width of the specified DOM element(s).
Syntax:
$('selector expression').width(value);
Specify a selector to get the reference of an element and call width() method to get the
width in pixel. Specify width as integer parameter to set the width.
<p>
This is paragraph.
</p>
Syntax:
$('selector expression').offset(options);
Specify a selector to get the reference of an element and call offset() method to get the
jQuery object which has left and top property. Specify JSON object with left and top
property with the coordinates where you want to move the element.
<p>
This is paragraph.
</p>
24
DCIT 50 – Object-Oriented Programming hayperaktib
Lecture # 1 jQuery Introduction + Event Binding
Points to Remember :
25