Introduction To Jquery: Maktab Sharif Front-End Bootcamp Spring - 2018 Alireza Riahi and Ramin Afhami
Introduction To Jquery: Maktab Sharif Front-End Bootcamp Spring - 2018 Alireza Riahi and Ramin Afhami
• What has made jQuery so popular is it’s ease of use, with selections
resembling CSS and a comprehensible separation of behavior.
• The benefits of jQuery are massive, however for our purpose we will only be
considered about the ability to find elements and perform actions with them.
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
<head>
<title>jQuery Demo</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src=“js/jquery-3.3.1.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>
Maktab Sharif - spring 2018 Alireza Riahi and Ramin Afhami
Getting Started with jQuery
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
• This also offers an advantage that if the visitor to your webpage has already
downloaded a copy of jQuery from the same CDN, it won't have to be re-
downloaded.
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
jQuery is a JavaScript file that you will link to in your HTML. There are two ways
to include jQuery in a project:
<head>
<title>jQuery Demo</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="js/scripts.js"></script>
</body>
</html>
Maktab Sharif - spring 2018 Alireza Riahi and Ramin Afhami
Getting Started with jQuery
• jQuery Object
jQuery comes with it’s own object, the dollar sign, $, also known as jQuery. The
$ object is specifically made for selecting an element and then returning that
element node to perform an action on it. These selections and actions should be
written in a new file, referenced outside of the actual jQuery library.
$()
Script.js
jQuery()
• Document Ready
Before trigging any jQuery to traverse and manipulate a page it is best to wait
until the DOM is finished loading. By placing all of our other custom written
jQuery inside of this function we can guarantee that it will not be executed until
the page has loaded and the DOM is ready.
$(document).ready(function() {
// jQuery code
Script.js
});
We're now going to put our custom "Hello, World!" program inside the jQuery
ready() wrapper.
<body>
<p id="demo"></p> Index.html
…
$(document).ready(function() {
$("#demo").html("Hello, World!"); Script.js
});
Once you've saved the file, you can open your index.html file in your browser. If
everything worked properly, you will see the output Hello, World!.
• Selectors are how we tell jQuery which elements we want to work on.
• Most jQuery selectors are the same as what you're familiar with in CSS, with a
few jQuery-specific additions.
• jQuery selectors are used to "find" (or select) HTML elements based on their
name, id, classes, types, attributes, values of attributes and much more.
• You can view the full list of jQuery selectors on their official documentation
pages.
$("selector")
• Double-quoted strings are preferred by the jQuery Style guide, though single-
quoted strings are often used as well.
$(".example") Class selects every element that has the example class
applied to it
$("#example") Id selects a single instance of the unique example id.
$(‘p, i') Multiple selects every instance of the <p> and <i> tags
$("div").click(function(event) {
$("this").hide();
});
• Traversing
jQuery provides a handful of methods for traversing up and down the DOM tree,
filtering and selecting elements as necessary.
To get started with filtering elements inside the DOM a general selection needs
to be made, from which will be traversed from relatively. In the example below
the original selection finds all of the div elements in the DOM, which are then
filtered using the .not() method. With this specific method all of the div elements
without a class of type or collection will be selected.
$("div").not(".type, .collection ");
• Chaining Methods
For even more control as to which elements are selected different traversing
methods may be chained together simply by using a dot in-between them.
The code sample below uses both the .not() method and the .parent() method.
Combined together this will only select the parent elements of div elements
without a class of type or collection.
$("div").not(".type, .collection ").parent();
• Traversing Methods
jQuery has quite a few traversing methods available to use. In general, they all
fall into three categories, filtering, miscellaneous traversing, and DOM tree
traversing. The specific methods within each category may be seen below.
1. Filtering
<div></div>
<div id="blueone"></div>
<div></div>
<div class="green"></div> Index.html
<div class="green"></div>
<div class="gray"></div>
<div></div>
$( "div" ).not( ".green, #blueone" )
Script.js
.css( "border-color", "red" );
• Traversing Methods
jQuery has quite a few traversing methods available to use. In general, they all
fall into three categories, filtering, miscellaneous traversing, and DOM tree
traversing. The specific methods within each category may be seen below.
1. Filtering
Demo
Demo
• .first(): Reduce the set of matched elements to the first in the set.
• .has(): Reduce the set of matched elements to those that have a descendant
that matches the selector or DOM element.
• .is(): Check the current matched set of elements against a selector, element,
or jQuery object and return true if at least one of these elements matches the
given arguments.
• .last(): Reduce the set of matched elements to the final one in the set.
• .map(): Pass each element in the current matched set through a function,
producing a new jQuery object containing the return values.
• .add(): Create a new jQuery object with elements added to the set of matched
elements.
<div></div>
<div></div>
<div></div>
<div></div> Index.html
<div></div>
<div></div>
<p>Added this... (notice no border)</p>
$( "div" ).css( "border", "2px solid red" )
.add( "p" ) Script.js
.css( "background", "yellow" );
Demo
Demo
• .addBack(): Add the previous set of elements on the stack to the current set,
optionally filtered by a selector.
• .andSelf(): Add the previous set of elements on the stack to the current set.
• .end(): End the most recent filtering operation in the current chain and return
the set of matched elements to its previous state.
• .find(): Get the descendants of each element in the current set of matched
elements, filtered by a selector, jQuery object, or element.
• .find(): Get the descendants of each element in the current set of matched
elements, filtered by a selector, jQuery object, or element.
Demo
• .parent(): Get the parent of each element in the current set of matched
elements, optionally filtered by a selector.
• .parent(): Get the parent of each element in the current set of matched
elements, optionally filtered by a selector.
Demo
• .closest(): For each element in the set, get the first element that matches the
selector by testing the element itself and traversing up through its ancestors in
the DOM tree.
• .next(): Get the immediately following sibling of each element in the set of
matched elements. If a selector is provided, it retrieves the next sibling only if
it matches that selector.
• .nextAll(): Get all following siblings of each element in the set of matched
elements, optionally filtered by a selector.
• .nextUntil(): Get all following siblings of each element up to but not including
the element matched by the selector, DOM node, or jQuery object passed.
• .prev(): Get the immediately preceding sibling of each element in the set of
matched elements. If a selector is provided, it retrieves the previous sibling
only if it matches that selector.
• .prevAll(): Get all preceding siblings of each element in the set of matched
elements, optionally filtered by a selector.
• .prevUntil(): Get all preceding siblings of each element up to but not including
the element matched by the selector, DOM node, or jQuery object.
• .siblings(): Get the siblings of each element in the set of matched elements,
optionally filtered by a selector.
Maktab Sharif - spring 2018 Alireza Riahi and Ramin Afhami
Manipulation
Selecting and traversing elements in the DOM is only part of what jQuery offers,
one other major part is what is possible with those elements once found. One
possibility is to manipulate these elements, by either reading, adding, or
changing attributes or styles. Additionally, elements may be altered in the DOM,
changing their placement, removing them, adding new elements, and so forth.
1. Attribute Manipulation
One part of elements able to be inspected and manipulated are attributes. A few
options include the ability to add, remove, or change an attribute or its value.
• .attr(): Get the value of an attribute for the first element in the set of matched
elements or set one or more attributes for every matched element.
var a = $('img').attr('alt');
Script.js
$("img").attr("width", "500");
Demo
Demo
• .prop(): Get the value of a property for the first element in the set of matched
elements or set one or more properties for every matched element.
• .toggleClass(): Add or remove one or more classes from each element in the
set of matched elements, depending on either the class’s presence or the
value of the state argument.
• .val(): Get the current value of the first element in the set of matched elements
or set the value of every matched element.
• .css(): Get the value of a computed style property for the first element in the
set of matched elements or set one or more CSS properties for every matched
element.
• .height(): Get the current computed height for the first element in the set of
matched elements or set the height of every matched element.
• .innerHeight(): Get the current computed inner height (including padding but
not border) for the first element in the set of matched elements or set the inner
height of every matched element.
• .position(): Get the current coordinates of the first element in the set of
matched elements, relative to the offset parent.
• .scrollLeft(): Get the current horizontal position of the scroll bar for the first
element in the set of matched elements or set the horizontal position of the
scroll bar for every matched element.
• .width(): Get the current computed width for the first element in the set of
matched elements or set the width of current vertical position every matched
element.
• .after(): Insert content, specified by the parameter, after each element in the
set of matched elements.
• .appendTo(): Insert every element in the set of matched elements to the end
of the target.
• .empty(): Remove all child nodes of the set of matched elements from the
DOM.
• .html(): Get the HTML contents of the first element in the set of matched
elements or set the HTML contents of every matched element.
• .insertAfter(): Insert every element in the set of matched elements after the
target.
• .insertBefore(): Insert every element in the set of matched elements before the
target.
• .replaceWith(): Replace each element in the set of matched elements with the
provided new content and return the set of elements that was removed.
• .text(): Get the combined text contents of each element in the set of matched
elements, including their descendants, or set the text contents of the matched
elements.
• .unwrap(): Remove the parents of the set of matched elements from the DOM,
leaving the matched elements in their place.
• .wrap(): Wrap an HTML structure around each element in the set of matched
elements.
Maktab Sharif - spring 2018 Alireza Riahi and Ramin Afhami
Manipulation
• .wrapAll(): Wrap an HTML structure around all elements in the set of matched
elements.
Demo
<p>This is a paragraph.</p>
<ol>
Index.html
<li> List item 1</li>
</ol>
$("p").append(" Appended text."); Script.js
$("ol").append("<li> Appended item </li>");
Demo
1. Browser Events
.resize() .scroll()
2. Document Loading
.ready()
4. Event Object
event.currentTarget event.preventDefault() event.stopPropagation()
event.target event.type
5. Form Events
.blur() .change() .focus() .select() .submit()
6. Keyboard Events
.focusin() .focusout() .keydown() .keypress() .keyup()
7. Mouse Events
.click() .dblclick() .focusin() .focusout() .hover() .mousedown()
.mouseenter() .mouseleave() .mousemove() .mouseout()
.mouseover() .mouseup()
$("p").on("click", function(){
$(this).hide();
});
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
}
});
1. Effect Syntax
each effect method has it’s own syntax which can be found in the jQuery effects
documentation. The duration, easing, and callback parameters outlined here are
common, but not available on every methoded, each effect method has it’s own.
• Basic Effects
.hide() .show() .toggle()
• Custom Effects
.animate() .clearQueue() .delay() .dequeue() jQuery.fx.interval
jQuery.fx.off .queue() .stop()
1. Effect Syntax
each effect method has it’s own syntax which can be found in the jQuery effects
documentation. The duration, easing, and callback parameters outlined here are
common, but not available on every methoded, each effect method has it’s own.
• Fading Effects
.fadeIn() .fadeOut() .fadeTo() .fadeToggle()
• Sliding Effects
.slideDown() .slideToggle() .slideUp()
$('.error').show('slow', 'linear');
$('.error').show(500, 'linear');
www.w3schools.com