CS3220 Web and Internet Programming Client-Side JavaScript and jQuery

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

Getting Started with jQuery. 1. Introduction to jQuery 2. Selection and DOM manipulation Contents 2.
JavaScript & jQuery JavaScript and jQuery introduction.
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,
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
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.
Tutorial 16 Working with Dynamic Content and Styles.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Philly.NET Hands-On jQuery + Plug-ins Bill Wolff, Rob Keiser.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
PhpXperts What is jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
DOM and JavaScript Aryo Pinandito.
Lecture 11 – DOM Scripting SFDV3011 – Advanced Web Development Reference: 1.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
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.
Unleash the Power of jQuery Doncho Minkov Telerik Software Academy academy.telerik.com Senior Technical Trainer
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
LOGO sparcs.org 1 jQuery Tutorial Presenter ㅎㅇㅎㅇ.
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
1 Javascript DOM Peter Atkinson. 2 Objectives Understand the nature and structure of the DOM Add and remove content from the page Access and change element.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
Review of the DOM Node properties and methods Some ways of accessing nodes Appending, copying and removing nodes Event handling – Inline – Scripting –
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.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
Unit 13 –JQuery Basics Instructor: Brent Presley.
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 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
JQuery The Write Less, Do More, JavaScript Library.
.. WHAT IS JQUERY JQuery is a fast, small, and feature-rich JavaScript library. Simplifies the interaction between HTML and JavaScript. The jQuery library.
Introduction to JavaScript DOM Instructor: Sergey Goldman.
CS520 Web Programming Introduction to Ajax and jQuery Chengyu Sun California State University, Los Angeles.
XHTML Forms.
THE DOM.
JQuery.
Introduction to.
DHTML.
Unit M Programming Web Pages with
CS5220 Advanced Topics in Web Programming More jQuery
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
CS 371 Web Application Programming
CS3220 Web and Internet Programming HTML Tables and Forms
Unit M Programming Web Pages with
CGS 3066: Web Programming and Design Spring 2017
Introduction to Web programming
Basic XHTML Tables XHTML tables—a frequently used feature that organizes data into rows and columns. Tables are defined with the table element. Table.
JQUERY Online TRAINING AT GOLOGICA
Document Object Model (DOM): Objects and Collections
DHTML Javascript Internet Technology.
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
DHTML Javascript Internet Technology.
Working with Dynamic Content and Styles
© 2015, Mike Murach & Associates, Inc.
..
Javascript and JQuery SRM DSC.
An introduction to jQuery
E-commerce Applications Development
Web Programming Language
JavaScript and Events CS Programming Languages for Web Applications
Chengyu Sun California State University, Los Angeles
JavaScript and Events CS Programming Languages for Web Applications
Presentation transcript:

CS3220 Web and Internet Programming Client-Side JavaScript and jQuery Chengyu Sun California State University, Los Angeles

Client-Side JavaScript Example Event handler onclick See Section 6.1.5.2, HTML 5 Specification Embed JavaScript in HTML using the <script> tag Link to external file or enclose code directly Can appear any number of times in both <head> and <body> HTML 5 no longer requires the type attribute Run inside a browser

Processing an HTML Document <html> <head><title>JavaScript Example</title></head> <body> <h1>JavaScript Example</h1> <p>Some content.</p> </body> </html> As a text file – very difficult As an object

Document Object Model (DOM) Representing documents as objects so they can be processed more easily by a programming language

DOM Representation Common terminology: document <html> <head> <body> <title> <h1> <p> “JavaScript Example” “JavaScript Example” “Some content.” Common terminology: parent, child, sibling, ancestor, descendant

About DOM Browser is responsible for parsing HTML document and creating the corresponding DOM object Changes to the DOM object are reflected on the page display

Manipulate a Document Find Elements Modify Elements Create Elements

Find Elements document.getElementById() document.getElementsByName() document.getElementsByTagName()

Modify Elements ... HTMLElement properites and methods IE innerHTML innerText insertAdjacentHTML() insertAdjacentText() Netscape/Mozilla Element-specific

... Modify Elements node setAttribute(), removeAttribute() appendChild(), removeChild() insertBefore(), replaceChild()

Create Elements document createElement() createTextNode()

Problems with Plain Client-Side JavaScript Verbose code for even simple tasks Browser compatibility issues

jQuery: Make Client-Side JavaScript Great Again Hide browser incompatibility Greatly simplify/improve DOM operations, event handling, and asynchronous request and response Usage statistics - https://trends.builtwith.com/javascript/javascript-library

jQuery Example Usage jQuery wrapper Selectors Elements Events and event handling DOM Manipulation

jQuery Wrapper jQuery() or $() Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string. $( "input[name='firstName']" ) $( "#who" ) $( "#t1" ) Selector

Basic Selectors By id By tag name By CSS class By attribute $(“#foo”) $(“div”) By CSS class $(“.foo”) By attribute $(“[name]”) $(“[name=‘joe’]”)

Combine Selectors Select all the <div> elements with CSS class foo and an attribute bar $(“div.foo[bar]”) Select all the <div> elements, and all the elements with CSS class foo, and all the elements with an attribute bar $(“div,.foo,[bar]”)

What Can We Do With An Element Get and set html(), text() attr() prop() val() Manipulate CSS addClass() removeClass() toggleClass() hasClass() Property tagName <input name=“username” value=“cysun” /> Attribute name val()

Typical Event and Event Handling in jQuery Event Handler $("#click").click( function(){ ... ... }); Unobtrusive JavaScript: separate style, behavior, and structure. <button id="click“ onclick="display();"> Click Me</button>

Other Events Mouse events Keyboard events Form events Browser events .click() .dbclick() … Keyboard events .keyup() .keydown() .keypress() Form events .change() .submit() … Browser events .resize() Document events

Document Ready Event Triggered when the DOM hierarchy of the HTML document is fully constructed $( document ).ready( handler ) $().ready( handler ) (not recommended) $( handler )

DOM Manipulation Insertion Removal Replacement Around (i.e. parent) Inside (i.e. children) append() … Outside (i.e. sibling) Removal remove() … Replacement Example: $("#t1").append("<tr><td>John</td><td>Doe</td></tr>");

Example: Language Question What program languages do you know? + - -

Attribute Selectors Exists Value match Value not match Prefix match $(“[name]”) Value match $(“[name=‘a’]”) Value not match $(“[name!=‘a’]”) Prefix match $(“[name|=‘a’]”) Contains word ~= Contains substring *= Starts with ^= Ends with $= All comparisons are case-sensitive.

Form Selectors :input – all input, textarea, select and button elements :button, :checkbox, :file, :image, :password, :radio, :reset, :submit, :text :checked for checkboxes and radio buttons :selected for <option> :enabled :disabled :focused

Hierarchical Selectors Children $(“parent > children”) Descendants $(“ancestor descendants”) Next sibling $(“prev + next”) Next siblings $(“prev ~ siblings”) There is no parent or ancestor selector.

Filters Applied to selectors to further refine the selection Examples: $(“table:last”) $(“p:contains(‘hello’)”) $(“*:hidden”) $(“:hidden”)

Index-Based Filters :first, :last, :even, :odd :eq(n) – nth elements :gt(n) – all elements after the nth element :lt(n) – all elements before the nth elements Index starts from 0.

Child Filters http://api.jquery.com/category/selectors/child-filter-selectors/ <table> <tr><td>1</td><td>2</td><td>3</td></tr> <tr><th>a</th><td>b</td><td>c</td></tr> </table> $(“td:first”) $(“td:eq(1)”) $(“td:first-child”) $(“td:first-of-type”) $(“td:nth-child(1)”) $(“td:nth-of-type(1)”)

Other Filters Contains text Contains element Has child Has no child :has(selector) Has child :parent Has no child :empty No matching :not(selector) State-based filters :focused :animated :hidden :visible

Access Selected Elements .each(function) iterate through the selected elements function(index, element) index: the index of the current element element: current element this: current element The difference between element, this, and $(this)?? .get(index) and .get() retrieve one or all of the selected elements (as an array)

Example: Tic Tac Place X and O alternately on the board Count Place X and O alternately on the board Count the number of X’s and O’s on the board Using :contains() Using .each(function)

About jQuery Object … $(selector)  jQuery object A jQuery object may contain multiple HMTL elements, e.g. $(“td”) Most of jQuery object methods apply to each of the selected elements, e.g. $(“td”).click(function(){}) $(“tr”).append(“<td></td>”) .each(function) can be used to apply a user-defined function to each of the selected element

… About jQuery Object Distinguish between HTML elements and jQuery objects this in an event handler or .each(function) is an HTML element Wrapping $() around an HTML element turns it into a jQuery object (so we can use jQuery object methods like .click(), .append(), and so on)

Traversing From selected elements to more (or less) elements Examples: $(“#cell”).closest(“tr”) $(“#row”).find(“td[name=‘foo’]”) $(“div”).filter(“.foo”)

Tree Traversal Find one or more siblings Find children Find parent Find descendants .find() Find Ancestors .parents() Find closest ancestor .closest() Find one or more siblings .prev() .prevAll() .next() .nextAll() .siblings() All methods take an optional selector argument.

Filtering “Filter” is part of selector; “filtering” applies to the selected elements .first(), .last() .eq(), .slice() .has(), not() .filter() – filter by selector, function, or elements

Effects .hide(), .show(), .toggle() Sliding effects Fading effects Custom effects

Example: Taking Attendance Absence Week 1 Week 2 Week 3 John 1 Jane 2 Tom

jQuery Tips Use CSS class to “label” certain elements to make selecting them easier (and the code more readable), e.g. $(“td.absence”) Most jQuery methods return the jQuery object itself so we can chain multiple method calls together, e.g. $(this).removeClass(“green”).addClass(“red”) $(this).prevAll().filter(“.absence”) $(“div”).append(“<p>first</p>”).append(“<p>second</p>”)