06 - jQuery
06 - jQuery
Introduction to jQuery
jQuery Syntax
jQuery Events
jQuery Effects
WHAT IS JQUERY?
There are several ways to start using jQuery on your web site.You can:
− Download the jQuery library from jQuery.com
− Include jQuery from a CDN, like Google
DOWNLOADING JQUERY
If you don't want to download and host jQuery yourself, you can include it from a CDN (Content Delivery
Network).
Both Google and Microsoft host jQuery.
To use jQuery from Google or Microsoft, use one of the following:
− Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
− Microsoft CDN:
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>
JQUERY SYNTAX
The jQuery syntax is tailor-made for selecting HTML elements and performing
some action on the element(s).
Basic syntax is: $(selector).action()
− A $ sign to define/access jQuery
− A (selector) to "query (or find)" HTML elements
− A jQuery action() to be performed on the element(s)
JQUERY EXAMPLES
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
MORE EXAMPLES OF JQUERY SELECTORS
Syntax Description
$("*") Selects all elements
$(this) Selects the current HTML element
$("p.intro") Selects all <p> elements with class="intro"
$("p:first") Selects the first <p> element
$("ul li:first") Selects the first <li> element of the first <ul>
$("ul li:first-child") Selects the first <li> element of every <ul>
$("[href]") Selects all elements with an href attribute
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"
$(":button") Selects all <button> elements and <input> elements of type="button"
$("tr:even") Selects all even <tr> elements
$("tr:odd") Selects all odd <tr> elements
JQUERY EVENT METHODS
All the different visitor's actions that a web page can respond to are called events.
Examples:
− moving a mouse over an element
Mouse Events Keyboard Events Form Events Document/Wind
− selecting a radio button ow Events
$("p").click(function(){
// action goes here!!
});
JQUERY EXAMPLES
$("#hide").click(function(){ $("button").click(function(){
$("p").hide(); $("p").hide(1000);
}); });
$("#show").click(function(){ $("button").click(function(){
$("p").show(); $("p").toggle();
}); });
JQUERY EFFECTS - SLIDING
Syntax:
$(selector).animate({params},speed,callback);
$("button").click(function(){
$("div").animate({left: '250px'});
});
JQUERY - FILTERS
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
});
});
});
</script>