SE-2840 Dr. Mark L. Hornick1 jQuery. jQuery is a library of JavaScript functions Helps minimize the amount of JavaScript you have to write in order to:

Slides:



Advertisements
Similar presentations
Diego Guidi - DotNetMarche. DOM tree is clunky to use No multiple handlers per event No high-level functions Browser incompatibilities = jQuery to the.
Advertisements

JavaScript – Quiz #8 Lecture Code:
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
24-Aug-14 HTML Forms. 2 What are forms? is just another kind of HTML tag HTML forms are used to create (rather primitive) GUIs on Web pages Usually the.
JavaScript and AJAX Jonathan Foss University of Warwick
The jQuery library. What is jQuery ? A javascript lightweight library Is very easy to use Is powerful Is cross-browser compatible Downloadable from jQuery.com,
Programming Club IIT Kanpur. Work environment Before you begin coding,always set up your work environment to your needs IDE Notepad++ Sublime Text 2.
CT-376 jQuery Most popular javascript library today Latest version:
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
The Document Object Model (DOM) 1 JavaScript is an object-based language—that is, it’s based on manipulating objects by changing each object’s properties.
JQuery CS 380: Web Programming. What is jQuery? jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling,
JQuery A Javascript Library Hard things made eas(ier) Norman White.
JQuery & SharePoint San Antonio Users Group – September Meeting September 22, 2009 Microsoft SharePoint Server.
Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
The Web Warrior Guide to Web Design Technologies
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
Chapter 9 Introduction to the Document Object Model (DOM) JavaScript, Third Edition.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
CST JavaScript Validating Form Data with JavaScript.
JQuery CS 268. What is jQuery? From their web site:
M. Taimoor Khan Courtesy: Norman White.
PhpXperts What is jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Selectors. Learning Objectives By the end of this lecture, you should be able to: – Select items using jQuery based on ID, class name, or HTML tag. –
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
Execution Environment for JavaScript " Java Script Window object represents the window in which the browser displays documents. " The Window object provides.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JQuery Adding behaviour…. Lecture Plan Review of last lesson Adding behaviour –click, mouseover Animation –fade, slideDown Navigation –parent, find, next.
JavaScript, Fourth Edition
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Animation & Effects Using JQuery. What is jQuery? jQuery is a lightweight, JavaScript library. The purpose of jQuery is to make it much easier to use.
JQuery Youn-Hee Han
CHAPTER 5 jQuery อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
JQuery JavaScript is a powerful language but it is not always easy to work with. jQuery is a JavaScript library that helps with: – HTML document traversal.
Web Programming Language Week 9 Dr. Ken Cosh Introducing jQuery.
Introduction to HTML. _______________________________________________________________________________________________________________ 2 Outline Key issues.
Intro to jQuery. What is jQuery? A JavaScript library Lightweight (about 31KB for the minified version) Simplifies HTML document traversing (DOM), event.
Web Programming Java Script & jQuery Web Programming.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
Unit 13 –JQuery Basics Instructor: Brent Presley.
Understanding JavaScript and Coding Essentials Lesson 8.
Introduction to JQuery COGS 187A – Fall JQuery jQuery is a JavaScript library, and allows us to manipulate HTML and CSS after the page has been.
CHAPTER 7 JQUERY WHAT IS JQUERY? jQuery is a script. It is written in JavaScript.
JQUERY AND AJAX
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
CIS 228 The Internet 12/6/11 Forms and Validation.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
JQuery Tutorial. What is jQuery jQuery is a JavaScript Library The purpose of jQuery is to make it much easier to use JavaScript on your website JavaScript.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Web Programming Java Script-Introduction. What is Javascript? JavaScript is a scripting language using for the Web. JavaScript is a programming language.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Tek Raj Chhetri Code for Humans not for machine.
Introduction to JavaScript Events
CGS 3066: Web Programming and Design Spring 2017
Introduction to Web programming
JQUERY Online TRAINING AT GOLOGICA
JavaScript & jQuery Timers, Effects, and Animations
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
JQuery Animation for beginners NOTES?!.
Web Programming Language
E-commerce Applications Development
Web Programming and Design
Presentation transcript:

SE-2840 Dr. Mark L. Hornick1 jQuery

jQuery is a library of JavaScript functions Helps minimize the amount of JavaScript you have to write in order to: Access DOM elements Modify the appearance of a web page Provide sophisticated animation effects Alter the content of a document Retrieve information from a web server without refreshing the page using Ajax Hides DOM differences between browsers Allows you to write browser-independent JavaScript! This was an even bigger deal a few years ago, but DOM differences between browsers still exist. SE-2840 Dr. Mark L. Hornick2

jQuery is open source Download from Note: two versions are available: jquery min.js – release version  “minified” (whitespace removed) - smaller, more compact  Takes less time to load your web page jquery js – development version Or just include the url to a CDN in your.html files: SE-2840 Dr. Mark L. Hornick3 Note: Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time. Also, most CDN's will make sure that once a user requests a file from it, it will be served from the server closest to them, which also leads to faster loading time.

Include the jQuery library before your Javascript code Your title <script src=" SE-2840 Dr. Mark L. Hornick4

jQuery philosophy/design approach is simple Focus on the interaction between JavaScript and HTML (Almost) every operation boils down to: 1. Find DOM element (e.g. paragraph, id, class…) 2. Do something to it (color it, move it, change it…) SE-2840 Dr. Mark L. Hornick5

jQuery syntax 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) Examples: $(this).hide() - hides the current element. $("p").hide() - hides all elements. $(".test").hide() - hides all elements with class="test". $("#test").hide() - hides the element with id="test". 6

jQuery is based on a selection mechanism that queries the DOM for various elements: In regular JavaScript: var el = document.getElementById(“hello”); el.innerHTML = “hello world”; Or document.getElementById(“hello”).innerHTML=“hello world”; Same thing in jQuery: $(“#hello”).html(“hello world”); SE-2840 Dr. Mark L. Hornick7 All selections in jQuery begin with $( selector ), with the selection argument(s) in the parentheses

What’s that $ about ?? Almost everything starts with a call to the jQuery() selector function. Since it’s called so often, the $ global variable is defined as an alias to the function: var $ = jQuery; //shortcut to jQuery function Thus you could write either: $(“#p2”).html(“hello world”); Or jQuery(“#p2”).html(“hello world”); SE-2840 Dr. Mark L. Hornick8

$( selector ) selector can be one of the following: A CSS selector expression A string of HTML A JavaScript object SE-2840 Dr. Mark L. Hornick9 We’ll focus mainly on the first kind

jQuery CSS selector syntax is more general and powerful than JavaScript selector syntax $(“#goButton”) // ~getElementById(“goButton”) $(“.index”) // ~getElementsByClassName(“index”) $(“p”) // ~getElementsByTagName(“p”) $(“p.abc em”) // gets ALL ’s in ’s of class abc $(“p, h1”) // gets ALL and elements $(“:button”) // gets ALL ’s of type “button” $(“:contains(‘hi ’)”) // all elements containing text “hi” $(“p:contains(‘hi ’)”) // all elements containing “hi” SE-2840 Dr. Mark L. Hornick10 Ref:

More examples of jQuery Selectors 11 See

$(selector). () An () method typically follows the jQuery selector, specifying what operation to perform on the selected element(s) Examples: $(“p”).hide(); // hides ALL p’s (implied iteration) $(“#p1”).fadeOut(); // fades element with id of #p1 $(“.xyz”).css({“color”:”blue”}); // changes CSS on all elements of class “xyz” (implied iteration) SE-2840 Dr. Mark L. Hornick12 Note the CSS-like syntax – although items have to be quoted

Implied Iteration SE-2840 Dr. Mark L. Hornick13 A selector such as $(“p”) that select more than one element automatically invokes the action for each element: // Hide ALL elements on the page $(“p”).hide(); // Change the color on ALL elements of class “xyz” $(“.xyz”).css({“color”:”blue”}); // Change font size on ALL button elements $(“:button”).css({“font-size”:”36px”}); // hide all button elements of class “xyz” $(“.xyz:button”).hide();

Actions for simple effects SE-2840 Dr. Mark L. Hornick14 See

Hiding and showing elements $(“h1”).hide(); // immediately hides ALL h1 $(“#p1”).hide(“slow”); // slowly hides #p1 $(“.xyz”).hide(“fast”); $(“p”).hide(5000); // user-specified rate $(“h1:first”).show(); // immediately show FIRST h1 $(“h1:first”).toggle(); // show or hide SE-2840 Dr. Mark L. Hornick15

Sliding: another way of hiding and showing elements $(“h1”).slideUp(); // hide at a default rate $(“#p1”).slideUp(“slow”); $(“.xyz”).slideUp(“fast”); $(“p”).slideUp(5000); $(“h1:first”).slideDown(); // show $(“h1:first”).slideToggle(); // show or hide hide(), show() with no arguments change the visibility immediately – no animation is provided slideUp() and slideDown() even with no arguments provide default animation SE-2840 Dr. Mark L. Hornick16

Fading elements in and out $(“h1”).fadeOut();// h1 fades at default rate (400ms) $(“#p1”).fadeOut(“slow”); // slowly fades out $(“p”).fadeOut(5000); // user-specified fade rate $(“h1:first”).fadeIn(); // fadesIn at default rate hide(), show(), and toggle() work by shrinking and expanding the height of the selected element(s) fadeIn() and fadeOut() operate by varying the opacity of the selected element(s) SE-2840 Dr. Mark L. Hornick17

jQuery Effect Methods Reference:

Method chaining $(“h1”).fadeOut().fadeIn(); fadeIn() executes after fadeOut() completes. fadeOut() and fadeIn() are placed in jQuery’s animation queue, which processes actions in sequence. Note: This is similar to using Java’s invokeLater() method to execute operations in sequence on the Event Dispatch Thread SE-2840 Dr. Mark L. Hornick19

Iteration filters let you specify exactly which elements in a selector are affected: SE-2840 Dr. Mark L. Hornick20 // Hide only the first element on the page $(“p:first”).hide(); // Hide only the last element on the page $(“p:last”).hide(); // Hide the 2 nd element on the page // Note: 0-based indexing $(“p:eq(1)”).hide(); // Change the color on all elements of class “.xyz” $(“.xyz”).css({“color”:”blue”}); // Change the color only on the even rows of a table $(“tr:even”).css({“color”:”blue”}); See

Iterating through multiple elements SE-2840 Dr. Mark L. Hornick21 This is paragraph 1. This is paragraph 2. In jQuery: $(“p”).each( function(index) { // the index is automatically incremented by // jQuery with each iteration if( index == 0 ) { // code for 1 st goes here… } // $(this) is the current jQuery object in the iteration if( $(this).text().indexOf(“2”) !== -1 ) { //contains “2”? // code for 2 nd goes here… } if( $(this).is(“#p2”) ) { // is current element #p2? …}

$(this) vs. this $(this) is a jQuery object You call jQuery methods on it $(this).html(“hello”); this is the underlying DOM object You call DOM methods on it this.innerHTML = “hello”; $(this)[0] == this; $(“#p2”)[0] == document.getElementById(“p2); SE-2840 Dr. Mark L. Hornick22

Event Handling SE-2840 Dr. Mark L. Hornick23

jQuery Event Methods User actions that a web page can respond to are called events: moving a mouse over an element selecting a radio button clicking on an element 24 See

window.onload event handling – the jQuery equivalent In regular JavaScript: window.onload = init; // or window.document.body.onload=init; function init() { // init code here… } In jQuery: $(window).load(init); or $(document).ready(init);//preferred! $().ready( function() {…}); // alt SE-2840 Dr. Mark L. Hornick25

$(document).ready vs $(window).load $(window).load executes after your pages loads, including any images your page may display. Browsers load images on separate threads, since image loading can take a long time. $(document).ready executes as soon as the browser completes DOM creation, which occurs (typically) before all of the images are loaded. Your jQuery script can thus start executing more quickly after a user browses to your page. SE-2840 Dr. Mark L. Hornick26

Handling events from input elements In regular JavaScript: var button = document.getElementById(“okButton”); button.onclick = function() { // button code here… } In jQuery: $(“#okButton”).click( function() { // button code here… } SE-2840 Dr. Mark L. Hornick27

example $(“p”) – select all elements on a page: $().ready(function(){ $("button").click(function(){ $("p").hide(); // hides all elements on button press }); This is a heading This is a paragraph. This is another paragraph. Click me 28 Anonymous function

example $(document).ready(function(){ // ready event $("button").click(function(){ // click event $("p").hide(); }); $("#p1").mouseenter(function(){// mouse enter event alert("You entered p1!"); }); $("#p1").mouseup(function(){// mouse up event alert("Mouse up over p1!"); }); $("#p1").hover(function(){// hover event alert("You entered p1!"); }, function(){ alert("You left p1!"); }); }); 29 Useful events: click() dblclick() mouseenter() mouseleave() mousedown() mouseup() hover()

Handling events from multiple elements SE-2840 Dr. Mark L. Hornick30 In jQuery: $(“:button”).click( function() { // handle ALL button clicks if( $(this).is(“#okButton”) { // $(this) is current element // code for handling okButton goes here… } if( $(this).is(“#quitButton”) { // code for handling quitButton goes here… } }

Callbacks SE-2840 Dr. Mark L. Hornick31

Callback functions can be supplied to execute after the action takes place function doIt() {alert(“hid it!”);} // doIt executes after each hide() completes $(“h1”).hide(doIt); $(“#p1”).hide(“slow”, doIt); // Anonymous function executes after // show() completes $(“.xyz”).show(“fast”, function(){alert(“made it visible!”);} );... SE-2840 Dr. Mark L. Hornick32

Custom animations SE-2840 Dr. Mark L. Hornick33

jQuery can automatically handle animations of elements Note: Elements to be animated have to be positioned either fixed, absolute, or relative. Static elements cannot be animated! Format of the animate method: $(“#p2”).animate({left:”100px”},500); SE-2840 Dr. Mark L. Hornick34 The first parameter specifies the CSS property you want to animate The second parameter specifies the duration in milliseconds

Stopping an animation or effect before it is finished Syntax: $(selector).stop([clearQueue],[jumpToEnd]); Immediately stops the currently-running animation. clearQueue – if false, the next animation in the chain begins; if true, all subsequent animations are cancelled. jumpToEnd – if true, the end result of the animation being stopped is applied Example with chaining: $("#p1").slideUp(2000).slideDown(2000); If the slideUp animation is executing when stop(false, false) is called, the slideUp animation stops immediately, and the slideDown animation begins executing. 35

Form Selectors 36

To select an input by type: To select a text input $('input:text'); To get the textbox value $('input:text').val(); // alternate: $(“#t1).val() To set the textbox value $('input:text').val("New Text"); 37 Other types are :textarea, :radio, :checkbox, :password, … See

Button selection by button type To select a submit button $('input:submit'); To select a reset button $('input:reset'); 38

Additional References jQuery Base ry.com%2Fmw%2Findex.php%3Ftitle%3DCore%26redirect%3Dno ry.com%2Fmw%2Findex.php%3Ftitle%3DCore%26redirect%3Dno jQuery Selectors 3Dno 3Dno jQuery Events uery.com%2Fmw%2Findex.php%3Ftitle%3DEvents%26redirect%3Dno uery.com%2Fmw%2Findex.php%3Ftitle%3DEvents%26redirect%3Dno Many examples of using selectors: