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.

Slides:



Advertisements
Similar presentations
17 HTML, Scripting, and Interactivity Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and.
Advertisements

JavaScript – Quiz #8 Lecture Code:
Chapter 3 – Web Design Tables & Page Layout
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.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
Unit 3 Day 4 FOCS – Web Design. No Journal Entry.
Cascading Style Sheets
Chapter 3 – Designing your web pages Dr. Stephanos Mavromoustakos.
CSS. CSS, or Cascading Styles Sheets, is a way to style HTML. Whereas the HTML is the content, the style sheet is the presentation of that document.
Modifying existing content Adding/Removing content on a page using jQuery.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
Need to define two things: › The destination › Something to click on to get there  Tag is click here  Can be text, special character or image (next.
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.
“Cascading Style Sheets” for styling WWW information DSC340 Mike Pangburn.
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
Dawn Pedersen Art Institute. What is Spry? Spry is Dreamweaver’s version of JavaScript libraries. Spry effects alter the look of a page element—or of.
Event Handling. Overview How to listen (and not listen) for events Creating a utility library (and why you should) Typology of events Pairing event listeners.
The Web Warrior Guide to Web Design Technologies
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
Web Page Development Identify elements of a Web Page Start Notepad
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
HTML Lesson 1 TBE 540 Farah Fisher. Prerequisites Before beginning this lesson, the student must be able to… Access web pages and navigate the web Access.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
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.
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
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.
Copyright © Texas Education Agency, All rights reserved. 1 Web Technologies Website Development with Dreamweaver.
KRAD makes it easy to handle HTML events in your webpage. Kuali University: Apply Now Lab 6: Fun with HTML Events Lab Objectives HTML Events – what are.
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. –
Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving.
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
Unit 3 Day 6 FOCS – Web Design. Journal Unit #3 Entry #4 Write the source code that would make the following display on a rendered page: Shopping List.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
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.
Events Part II Browser Variations Document / Window Events Form Events --- Using named functions (instead of anonymous)
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,
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKSTART GUIDE HTML and CSS 8th Edition Chapter 8: Working with Style Sheets.
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.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
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.
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
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.
Host Objects: Browsers and the DOM
Understanding JavaScript and Coding Essentials Lesson 8.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
1 Lesson 6 Introducing JavaScript HTML and JavaScript BASICS, 4 th Edition.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
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)
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 5 Host Objects: Browsers.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Introduction to JavaScript Events
Tutorial 6 Topic: jQuery and jQuery Mobile Li Xu
Events.
Removing events Stopping an event’s normal behavior
Web Programming and Design
Events Part I Mouse Events.
Events Part III The event object.
Modifying HTML attributes and CSS values
Random Stuff Colors Sizes CSS Shortcuts.
Using the API.
Retrieving information from forms
Presentation transcript:

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 the ‘ event ’ object – Improve your familiarity with the API

The hover() function The hover function is simply a function that serves as a replacement for having to write code separately for the mouseover and mouseout events. You could simply use those two events, but hover() is a shortcut. However, this function requires TWO functions as the arguments. The first function is the one that will kick in when the user moves the mouse over the selector. In other words, this is the same thing as writing the ‘ mouseover ’ event. The second function is the one that will kick in when the user moves the mouse off of the selector. In other words, this is the same thing as writing the ‘ mouseout ’ event. So instead of: $('h3').mouseover(function(){ $(this).css('color','red'); }); $('h3').mouseout(function(){ $(this).css('color', 'black'); //set back to normal color }); You could have: $('h2').hover( function() {//FIRST anonymous function – for mouseover $(this).css('color','red'); }, function() {//SECOND anonymous function – for mouseout $(this).css('color','black'); } //anonymous function for mouseout ); //closing ‘)’ for hover() function FILE: hover_with_menu.htm Do not forget this comma!!!

The ‘event’ object Whenever an event is generated or ‘fires’ (e.g. a click event or a keypress event), the browser records various information about that event. This information is stored inside something called an event object. For example, for a click event, the event object will store information such as the x and y (horizontal and vertical) coordinates of exactly where inside the browser window the mouse pointer was located when the mouse was clicked. It will also, of course, record what element the mouse was on when it was clicked (e.g. a button). Lots of other information will be recorded too such as as whether or not the user was simultaneously pressing the ‘shift’ key when the button was clicked. For keypress events, the event object will store relevant information such as which key was pressed, where on the screen the user was when the key was pressed (e.g. inside a text field) and so on. As you can hopefully see, the particular pieces of information that are encapsulated inside an event object, depend on the type of event that was generated. Here is a partial list of some properties available to the ‘event’ objects: pageX : The distance (in pixels) of the mouse pointer from the left edge of the browser window. pageY : The distance (in pixels) of the mouse pointer from the top of the browser window. target : The object on the web page that was the ‘target’ of the event. For a click, this would be the element that was clicked. which : For key events, this will tell you which key was pressed. For mouse events, will tell you which button was pressed (1 for left, 2 for middle, 3 for right). Here is the jQuery API documentation for the event object: It is important to note that (as always) these properties can vary between browsers. In other words, we must be aware that even jQuery sometimes falls victim to issues of consistency among browsers. Still, as the API documentation tells us, the properties discussed in the list above should work across all major browsers.

Example: Using the event object Here is some code that will simply alert the X and Y coordinates whenever the user clicks the button. $(document).click( function( evt ) { var xPos = evt.pageX; var yPos = evt.pageY; alert("X is at: " + xPos + ", and Y is at: " + yPos); }); Recall that with our previous anonymous functions, we did not provide any arguments to our anonymous function. In those examples, the identifier function( ) contained nothing inside the parentheses. However, if we want to use the event object, we must provide an argument to our anonymous function. The identifier I will give to this argument is ‘ evt ’. (I will avoid calling it ‘event’ since this is a reserved keyword in some languages). So in this case, our anonymous function will look like this: function(evt) Now we can use this ‘ evt ’ identifer to find out information about our mouse click event. And to reiterate, the identifier does not have to be evt – just be sure that you choose an identifier that is clear. I believe that ‘evt’ serves our purpose just fine, and many programmers use it. Also note in the example above, we use the selector ‘ document ’. This means that the entire web page serves as our selector instead of, say, a specific tag or ID.

Example 2: Using the event object Here is some code that will alert the name of any target on the page that has been clicked on: $(document).click( function(evt) { alert( evt.target ); }); Try placing this code inside a page with lots of different elements and look at the resulting output when you click. You will see that it will identify (in somewhat awkward text) whether the item was a heading tag, an input tag (e.g. text field, button, etc), or some other HTML element. You will not, of course, typically, ‘alert’ the item that was clicked. However, you can write JS/JQ code to respond to specific things if you need to. Though we won’t do it now, you could, for example, write code to determine if, say, the item clicked was a radio button, and could have code that responds differently than it would for some other kind of element.

Example of Using the API to learn about a topic: Keypress Event This example (with very slight modifications) is taken from the jQuery API. We first have a simple text field in which we ask the user to type something: Type Something: Note that just below the input tag is an empty div called ‘ log ’. We will output which key the user pressed in this section using jQuery’s html() function. The property ‘ which ’ is the one that tells us about which key triggered the event. Here is the code from the API documentation: $( "#whichkey" ).on( "keypress", function( event ) { $( "#log" ).html( event.type + ": " + event.which ); }); Now as has been discussed, you should try not to get thrown off or discouraged if you look at the API and encounter unfamiliar material. In this case, for example, the on() function is probably new to you. However, if you sort of gloss over the unfamiliarity for the moment, you should, with some close perusal, be able to work out what is going on. And of course, you can then simply look up the on() function to try and get an idea of how that function works! Similarly, the ‘ type ’ property tells us the name of the event that occurred, in this case, a ‘ keypress ’ event. Returning to the keypress event, note that the function returns a number. This is fairly standard in programming. In fact, the number returned follows a standard, called Unicode in which A is assigned 65, B is assigned 66 and so on. Lower case ‘a’ is assigned 97, ‘b’ is 98 and so on. If you type other characters including space, backspace, dollar sign, etc, etc, you will see that all of them have specific character codes. FILE: keypress_example.htm