Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ.

Slides:



Advertisements
Similar presentations
HTML Basics Customizing your site using the basics of HTML.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Modifying existing content Adding/Removing content on a page using jQuery.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lesson 12- Unit L Programming Web Pages with JavaScript.
Chapter 16 Dynamic HTML and Animation The Web Warrior Guide to Web Design Technologies.
4.01 Cascading Style Sheets
Web Design is a class created to nurture the minds of high school techies, and introduce those without prior knowledge to the field.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
DHTML. What is DHTML?  DHTML is the combination of several built-in browser features in fourth generation browsers that enable a web page to be more.
HTML & CSS A brief introduction. OUTLINE 1.What is HTML? 2.What is CSS? 3.How are they used together? 4.Troubleshooting/Common problems 5.More resources.
Week 1.  Phillip Chee   Ext.1214 
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
 2008 Pearson Education, Inc. All rights reserved Document Object Model (DOM): Objects and Collections.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
A really fairly simple guide to: mobile browser-based application development (part 4, JQuery & DOM) Chris Greenhalgh G54UBI / Chris Greenhalgh.
CHAPTER 3 Working with Templates and Styles. CHAPTER OBJECTIVES  Create an Expression Web site from a template  Rename a page  Rename a folder  Add.
UFCEWT-20-3 Advanced Topics in Web Development Lecture 4 : JavaScript, Browsers & the HTML DOM-API.
Images (1) Three most popular formats – Graphics Interchange Format (GIF) – Joint Photographic Experts Group (JPEG) – Portable Network Graphics (PNG) –
Title, meta, link, script.  The title looks like:  The tag defines the title of the document in the browser toolbar.  It also: ◦ Provides a title for.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
Creating an Animated Web Page
CITS1231 Web Technologies JavaScript and Document Object Model.
JavaScript, Fourth Edition
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
5.2 DOM (Document Object Model). 2 Motto: To write it, it took three months; to conceive it three minutes; to collect the data in it — all my life. —F.
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.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Javascript II DOM & JSON. In an effort to create increasingly interactive experiences on the web, programmers wanted access to the functionality of browsers.
Professor Waterman Cascading Style Sheets (CSS) is a language that works with HTML documents to define the way content is presented. The presentation.
 HTML is hypertext markup language. It is a way for people to display their information with more complex pictures and text displays. Before HTML, messages.
Definition CSS “Short for Cascading Style Sheets, a new feature being added to HTML that gives both Web site developers and users more control over how.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 10.
Web Development 101 Presented by John Valance
INTRODUCTION TO CSS. TOPICS TO BE DISCUSSED……….  Introduction Introduction  Features of CSS Features of CSS  Creating Style Sheet Creating Style Sheet.
Cs332a_chapt10.ppt CS332A Advanced HTML Programming DHTML Dynamic Hypertext Markup Language A term describing a series of technologies Not a stand-a-lone.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2 Revised by Dr. T. Tran for CSI3140.
DYNAMIC HTML What is Dynamic HTML: HTML code that allow you to change/ specify the style of your web pages. Example: specify style sheet, object model.
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.
TOPIC II Dynamic HTML Prepared by: Nimcan Cabd Cali.
Are You Smarter Than a 5 th Grader? 1,000,000 5th Grade HTML 5th Grade Syntax 4th Grade HTML 4th Grade Syntax 3rd Grade HTML 3rd Grade Syntax 2nd Grade.
DOM (Document Object Model) - Parsing and Reading HTML and XML -
INTRODUCTION JavaScript can make websites more interactive, interesting, and user-friendly.
Host Objects: Browsers and the DOM
Create Element, Remove Child. The Document Tree Document Element Root Element Element Element Element Element Text: HelloWorld Attribute “href”
Chapter 10 Dynamic HTML (DHTML) JavaScript, Third Edition.
XML DOM Week 11 Web site:
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)
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
Creating Web Documents CSS examples (do more later) Lab/Homework: Read chapters 15 & 16. Work on Project 3, do postings.
DHTML.
Programming Web Pages with JavaScript
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
4.01 Cascading Style Sheets
Unit M Programming Web Pages with
Document Object Model (DOM): Objects and Collections
Week 11 Web site: XML DOM Week 11 Web site:
Tutorial 6 Creating Dynamic Pages
Working with Dynamic Content and Styles
D3.js Tutorial (Hands on Session)
D3.js Tutorial (Hands on Session)
Lesson 5: HTML Tables.
Introduction to AJAX and JSON
4.01 Cascading Style Sheets
Presentation transcript:

Bringing pages to life with jQuery BEAR BIBEAULT YEHUDA KATZ

Bringing pages to life with jQuery Getting and setting element attributes Storing custom data on elements Manipulating element class names Setting the contents of DOM elements Storing and retrieving custom data on elements Getting and setting form element values Modifying the DOM tree by adding, moving, or replacing elements

Bringing pages to life with jQuery On an almost daily basis, many of us come across web pages that do something that makes us say, “Hey! I didn’t know you could do that!” And being the commensurate professionals that we are (not to mention being insatiably curious about such things), we immediately start looking at the source code to find out how they did it.

3.1 Working with element properties and attributes Properties are intrinsic to JavaScript objects, and each has a name and a value. The dynamic nature of JavaScript allows us to create properties on JavaScript objects under script control. Attributes aren’t a native JavaScript concept, but one that only applies to DOM elements. Attributes represent the values that are specified on the markup of DOM elements.

3.1 Working with element properties and attributes Consider the following HTML markup for an image element:

3.1 Working with element properties and attributes

3.1.1 Manipulating element properties jQuery doesn’t possess a specific method to obtain or modify the properties of elements. Rather, we use the native JavaScript notation to access the properties and their values. The trick is in getting to the element references in the first place.

3.1.1 Manipulating element properties Using array indexing on the wrapped set, as in $(whatever)[n] Using the get() method, which returns either an individual element by index, or toArray(), which returns an array of the entire set of elements Using the each() or map() methods, where the individual elements are made available in the callback functions

3.1.1 Manipulating element properties $('*').each(function(n){ this.id = this.tagName + n; });

3.1.2 Fetching attribute values Method syntax: attr(selector) method can be used either as a read or as a write operation.

3.1.2 Fetching attribute values $("#myImage").attr("data-custom")

3.1.3 Setting attribute values Method syntax: attr attr(name,value) Sets the named attribute to the passed value for all elements in the wrapped set.

3.1.3 Setting attribute values $('*').attr('title',function(index,previousValue) { return previousValue + ' I am element ' + index + ' and my name is ' + (this.id || 'unset'); });

3.1.3 Setting attribute values Method syntax: attr attr(attributes) $('input').attr({ value: '', title: 'Please enter a value' });

3.1.4 Removing attributes removeAttr(name) Removes the specified attribute from every matched element.

3.1.4 Removing attributes The removing an attribute doesn’t remove any corresponding property from the JavaScript DOM element, though it may cause its value to change. For example, removing a readonly attribute from an element would cause the value of the element’s readOnly property to flip from true to false, but the property itself isn’t removed from the element.

3.1.5 Fun with attributes EXAMPLE #1—FORCING LINKS TO OPEN IN A NEW WINDOW Some External Site

3.1.5 Fun with attributes EXAMPLE #2—SOLVING THE DREADED DOUBLE-SUBMIT PROBLEM $("form").submit(function() { $(":submit",this).attr("disabled", "disabled"); });

3.2 Changing element styling If we want to change the styling of an element, we have two options. We can add or remove a class, causing any existing style sheets to restyle the element based on its new classes. Or we can operate on the DOM element itself, applying styles directly.

3.2.1 Adding and removing class names addClass(names) Adds the specified class name or class names to all elements in the wrapped set. removeClass(names) Removes the specified class name or class names from each element in the wrapped set. toggleClass(names) Adds the specified class name if it doesn’t exist on an element, or removes the name from elements that already possess the class name.

3.2.1 Adding and removing class names function swapThem() { $('tr').toggleClass('striped'); } $(function(){ $("table tr:nth-child(even)").addClass("striped"); $("table").mouseover(swapThem).mouseout(sw apThem); });

3.2.2 Getting and setting styles css(name,value) Sets the named CSS style property to the specified value for each matched element. $("div.expandable").css("width",function(inde x, currentWidth) {return currentWidth + 20;});

3.2.2 Getting and setting styles width(value) height(value) Sets the width or height of all elements in the matched set. $("div.myElements").width(500) is identical to $("div.myElements").css("width",500)

3.2.2 Getting and setting styles function displayDimensions() { $('#display').html( $('#testSubject').width()+'x'+$('#testSubjec t').height() );

3.2.2 Getting and setting styles offset() Returns the position (in pixels) of the first element in the wrapped set relative to the document origin. position() Returns the position (in pixels) of the first element in the wrapped set relative to the element’s closest offset parent.

3.3 Setting element content Although use of the DOM API methods is certainly exact, it’s also fairly “wordy” and results in a lot of code, much of which can be difficult to visually inspect. In most cases, modifying an element’s HTML is easier and more effective, so jQuery gives us a number of methods to do so.

3.3.1 Replacing HTML or text content html() Obtains the HTML content of the first element in the matched set. text() Concatenates all text content of the wrapped elements and returns it as the result of the method.