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. –

Slides:



Advertisements
Similar presentations
Cross-Browser Compatibility. Myth or Reality? There's a long tradition to be overcome Sniffers to look for browser and OS combinations Separate code forks.
Advertisements

Anne McGrath 16 th February  Review of what we have learned so far.  Angled brackets surround HTML tags.  The words between the angled brackets.
Modifying existing content Adding/Removing content on a page using jQuery.
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.
CSW131 Steven Battilana 1 CSW 131 – Chapter 5 (More) Advanced CSS Prepared by Prof. B. for use with Teach Yourself Visually Web Design by Ron Huddleston,
Lesson 12- Unit L Programming Web Pages with JavaScript.
Copyright 2004 Monash University IMS5401 Web-based Systems Development Topic 2: Elements of the Web (g) Interactivity.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Russell Taylor Lecturer in Computing & Business Studies.
JQuery: Fun ‘n Games with Selectors. Learning Objectives By the end of this lecture, you should be able to: – Comfortably use jQuery to select based on.
Introduction to scripting
Random Stuff Colors Sizes CSS Shortcuts. Learning Objectives By the end of this lecture, you should be able to: – Identify the 3 most common ways in which.
Adobe Forms THE FORM ELEMENT PANEL. Creating a form using the Adobe FormsCentral is a quick and easy way to distribute a variety of forms including surveys.
Explanation of Web 6, Web 7 and Web 9 at my site Please be sure to bring up the speaker notes for the explanation Intro - Web 6, Web 7 and Web 9.
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
JavaScript & jQuery the missing manual Chapter 11
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
1 Direct Manipulation Proposal 17 Direct Manipulation is when physical actions are used instead of commands. E.g. In a word document when the user inputs.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
SEG3210 DHTML Tutorial. DHTML DHTML is a combination of technologies used to create dynamic and interactive Web sites. –HTML - For creating text and image.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Jquery Nasrullah. Jquery jQuery is a JavaScript Library. jQuery greatly simplifies JavaScript programming. jQuery is easy to learn.
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.
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.
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.
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Web Development 101 Presented by John Valance
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
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.
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.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Events Part I Mouse Events. Learning Objectives By the end of this lecture, you should be able to: – Understand what is meant by an ‘event’ – Appreciate.
IS2802 Introduction to Multimedia Applications for Business Lecture 07: Introduction to jQuery Rob Gleasure
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
Positioning Objects with CSS and Tables
Understanding JavaScript and Coding Essentials Lesson 8.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
JQUERY AND AJAX
JQuery is a fast, small, and feature-rich javascript library. It makes things like HTML document traversal and manipulation, event handling, animation,
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Getting Started With HTML
Unit M Programming Web Pages with
Tek Raj Chhetri Code for Humans not for machine.
Unit M Programming Web Pages with
Retrieving information from forms
HTML.
CSS: Classes and Contextual Selectors
CSS: Classes and Contextual Selectors
Events Part I Mouse Events.
Selectors.
Events Part III The event object.
Modifying HTML attributes and CSS values
Random Stuff Colors Sizes CSS Shortcuts.
JavaScript Part 2 Organizing JavaScript Code into Functions
Retrieving information from forms
CSS Classes.
Introduction to scripting
Presentation transcript:

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. – Apply some simple functions such as fadeIn(), fadeOut(), hide(), and show() to content that you have selected. – Demonstrate a very basic understanding of what the document object model (DOM) is. – Invoke the getElementsByTagName() and getElementById() JavaScript functions.

Why we use JS Scripting is easily the most powerful way we can turn a static web page into something much more dynamic and reactive. Some of the things we do with scripts include: Form validation: For example, if the user forgets to enter their phone number in a form, you can make sure that the page is not submitted until they do so. You might also confirm that their phone number does not contain any characters other than digits, parentheses, or dashes. Hiding/Showing things: For example, a common feature is to have items appear or disappear in response to a mouseover. In image shown here, a bubble appears when I mouse-over the ‘Breakfast at Tiffany’s’ title on Netflix. Animation/Positioning: You can animate and/or reposition elements on a web page as needed – even after the page has loaded. Appearance: You can cause sections to change their appearance based on timing, user behavior, form entry, and much more.

The basic process The basic process to manipulate content on a web page is to first select the content and then do something with the content you have selected. 1.Select an element or section: You can select all elements of a specific selector (e.g. all ‘h2’ tags), or you can fine-tune your selections. Here are some examples of elements/sections that you can select: o All ‘h2’ tags o All ‘h2’ tags that are colored green o All ‘a’ tags o All ‘a’ tags linked to google.com o A div section that has an id of ‘footer’ 2.Do something with the item you selected: Once you have made your selection, you can do all kinds of things with that selection. For example, suppose you selected a div section with an id of ‘quotation’. Things you might do with this selection could include: o Change the width of that section o Change its background color o Cause the selection to “dance” across the screen o Temporarily hide the section and only have it appear when the user moves the mouse over a certain image. o Cause an audio clip of the quotation to play in response to the user clicking on a certain icon. o Retrieve all of the text inside the quotation and convert it all to upper-case text.

Selecting content using JavaScript (as opposed to jQuery) Remember that jQuery, is essentially a ‘dialect’ of Javascript that provides us with all kinds of shortcuts for code that would take many, many lines of JS. Therefore, though we will be using jQuery the vast majority of the time, I will begin with a (very) brief overview on how to select items using JS. In particular, we will learn two JS functions for selecting content. After we have learned these two functions, we will focus on selecting content using jQuery. Selecting items using JS: Underlying every web page is something called the ‘Document Object Model’, or ‘DOM’. The DOM is part of the W3C’s standard for organizing the underlying structure of a web page. One of the main purposes of the DOM is to allow developers a way of accessing items on an HTML page using JS. Items in the DOM can be accessed in a variety of ways, one of which uses something called ‘array’ notation. For example, you can use JS to retrieve the third ‘p’ tag on the page and then find out the name of the first ‘descendant’ tag of that ‘p’ and change it’s appearance. (If that is confusing to you – don’t worry – it’s most definitely not something you’ll have to do. For now…) A much easier (though still sometimes problematic) way of retrieving content in JS is via the functions getElementsByTagName() and the closely related getElementById(). These two functions are the only Javascript tools that we will learn for selecting content. After that, we will focus mostly on selecting content using jQuery. However, do be sure you know how to do basic selecting using these functions.

The getElementById() and getElementsByTagName() functions Easiest way to demonstrate this is via an example: var temp = document.getElementsByTagName('p'); //temp now stores a link to all ‘p’ tags //on your page. If you know the JS command to, say, //change the color of ‘temp’, the content inside //all the ‘p’ tags would change color. document.getElementById('random_text').innerHTML="Hello!"; //Will take whichever tag has an ID of ‘random_text’ and //replace whatever HTML content is inside that tag with //the text Hello!! That’s all we are going to say about these functions for now. We may periodically return to them at some points throughout the course.

A (very) quick look at the DOM Working with the DOM requires understanding that all tags are either ancestors or descendants to other tags. In the example here, the tag is a child of the tag which is a child of the tag which is a child of the tag. The tag is the ancestor (the ‘Adam’ if you will) of ALL tags on the page. The good news? That’s all you need to understand about the DOM for now!!

Limitations of the DOM While the DOM’s primary purpose is standardization, in reality, different browsers frequently interpret the DOM differently. As a result, JS programmers have (in the past) had to write lots and lots of code to deal with the various different browsers. Once again, libraries such as jQuery come to the rescue. There is a tremendous amount of code written by jQuery developers that handles many of the messy details for us. – One important axiom of programming is: “Don’t reinvent the wheel.” In other words, if someone else has already written tried-and-true code to accomplish a certain task, you should certainly feel free to make use of it! – That being said, it is, of course, always important to learn the fundamentals of what is going on ‘under the hood’. Understanding the underlying theory is vital as it is the main tool that you have for debugging problematic web pages.

Selecting items the jQuery way jQuery also allows us to easily select items, however, it does so with some additional – and very important – control that is either unavailable or difficult to accomplish by using the getElementById() or getElementsByTagName() functions. Here is an overview of how we select items in jQuery: $('tag_or_selector_name') E.g. $('h2') $('#id_name') E.g. $('#citations') $('.className'); E.g. $('.emphasize') Note that jQuery commands begin with a $ sign. To select based on a selector (i.e. a tag name), simply name the selector. To select based on an ID (known as a contextual selector), you must precede the ID with a pound sign, ‘#’. To select based on a CSS class name, you must precede the identifier with a period ‘.’ All of these are placed inside quotes (we nearly always use single quotes)

“Doing” something with selected items Now that we have learned the very basic method selecting items using jQuery, let’s talk about doing something with selected items. There are many, many, jQuery functions that can be applied to a selected item. Obviously, which functions are available to a selected element depends on the particular element. For example, you can change the color of a ‘p’ tag, but not the color of a ‘meta’ tag. $('#random_text').fadeOut(2000); $('#random_text').fadeIn(2000); $('.emphasize').hide(); As you may have worked out, these lines of code will cause any content contained within an ID called ‘random_text’ to first fade out (over a period of 2000 milliseconds) and then to fade in, also over 2 seconds. The third line says to take every tag to which the class called ‘emphasize’ has been applied, and to make it invisible. FILE: jquery_select_modify.htm jquery_select_modify.htm

Common Pitfalls When you are first learning this material, the following mistakes are easy to make. Keep an eye out for them as it can save you significant debugging time! – Forgetting to place the ‘#’ before the identifier when you are trying to select based on an ID. – Forgetting to place the ‘.’ before the identifier when you are trying to select based on a class name. – Forgetting that when you select, you are selecting ALL items that match. For example, $(‘.emphasize’) will select ALL items to which the emphasize class has been applied.

Recap In this discussion we focused on selecting items based on the selector (tag), IDs (contextual selectors), and CSS classes. We then learned how to apply a couple of very simple jQuery functions to those selected items. We intentionally kept the functions very simple. In our upcoming discussions, we will, of course, learn how to apply more advanced and subtle selections and apply increasingly interesting/fun/advanced functions.