Event Handling © Copyright 2014, Fred McClurg All Rights Reserved.

Slides:



Advertisements
Similar presentations
JavaScript – Quiz #8 Lecture Code:
Advertisements

function coffeeinfo() { document.getElementById('p3').innerHTML = "The word 'coffee' was.
CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
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.
กระบวนวิชา 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.
CSS normally control the html elements. Three Ways to Insert CSS There are three ways of inserting a style sheet: External style sheet Internal style.
Session 11 Dynamic HTML: Event Model and Filters Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Javascript and dynamic doc presentation. Absolute positioning using CSS Absolute positioning /* A style for a paragraph of text */.regtext {font-family:
Cascading Style Sheets SP.772 May 6, CSS Useful for creating one unified look for an entire web site. Helps to seperate style from content. Can.
JavaScript Forms Form Validation Cookies CGI Programs.
Computer Science 103 Chapter 4 Advanced JavaScript.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Web Programming Material From Greenlaw/Hepp, In-line/On-line: Fundamentals of the Internet and the World Wide Web 1 Introduction The JavaScript Programming.
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.,
Tutorial #9 – Creating Forms. Tutorial #8 Review – Tables Borders (table, gridlines), Border-collapse: collapse; empty-cells: show; and rowspan, colspan.
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.
HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
DHTML: Dynamic HTML Internet Technology1. What is DHTML? A collection of enhancements to HTML ► To create dynamic and interactive websites Combination.
Intro to JavaScript. Use the tag to tell the browser you are writing JavaScript.
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.
1 Dynamic HTML III: Event Model Introduction Event model –Scripts respond to user actions and change page accordingly Moving mouse Scrolling screen.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Dynamic HTML: Event Model Outline 16.1Introduction 16.2Event ONCLICK 16.3Event ONLOAD.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
JavaScript Events with XHTML Please use speaker notes for additional information!
1 JavaScript 2 JavaScript. 2 Rollovers Most web page developers first use JavaScript for rollovers A rollover is a change in the appearance of an element.
Chapter 14: Dynamic HTML: Event Model Presented by: Colbie Brown CS340 Java Web Development Dr. Gloria Carter Love.
Event JavaScript's interaction with HTML is handled through events that occur when the user or browser manipulates a page. When the page loads, that is.
JQuery Introduction © Copyright 2014, Fred McClurg All Rights Reserved.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
More on Events Some More Events Image Rollover Objects Select-Option List Control 1.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 16 – Dynamic HTML: Event Model Outline 16.1Introduction 16.2Event ONCLICK 16.3Event ONLOAD.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 4 Dynamic HTML & CSS.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
JavaScript Challenges Answers for challenges
Event Handling. Objectives Using event handlers Simulating events Using event-related methods.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
 Scripts that execute in response to some event  User clicking on something  Script does NOT execute as part of page loading  DOM facilities like.
Web Programming Java Script & jQuery Web Programming.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
5 th and 4 th ed: some from chapters 9, 12, 13 SY306 Web and Databases for Cyber Operations Slide Set #8: Dynamic HTML.
Web Technologies Beginning Cascading Style Sheets (CSS) 1Copyright © Texas Education Agency, All rights reserved.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
JavaScript Events.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
Event Handlers Events are asynchronous. When an event occurs, JavaScript can execute code in response to the user’s action. This response to the user-initiated.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Dynamic HTML: Event Model Outline 14.1 Introduction 14.2 Event onclick 14.3 Event onload.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
OV Copyright © 2008 Element K Content LLC. All rights reserved. Working with Web Pages  An Introduction to Cascading Style Sheets  Format a Web.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
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.
Functions Wouldn’t it be nice to be able to bring up a new animal and paragraph without having to reload the page each time? We can! We can place the.
WEB SYSTEMS & TECHNOLOGY. Java Script  JavaScript created by Netscape  It is also client side programming  It can be use for client side checks.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
HTML Again GUI Items Originally HTML 4 Copyright © Curt Hill.
Event-Driven Programming
CSS Layouts: Grouping Elements
Introduction to JavaScript Events
JavaScript Events.
JavaScript and Ajax (JavaScript Events)
Presentation transcript:

Event Handling © Copyright 2014, Fred McClurg All Rights Reserved

What is an event? Discussion: An event is an action that is generated by HTML elements. JavaScript can respond to those events. 2

Element Event Handling Discussion: An in-line event can attached to most elements. Example: Books don't change people; paragraphs do, sometimes even sentences. -- John Piper 3 eventViaElement.html

Event via Anonymous Function Discussion: Event handlers can be attached to most elements. Anonymous functions are often used to define event actions. Example: Is is better to lose your life than to waste it. -- John Piper var para = document.getElementById( "clickMe" ); // define event handler para.onclick = function() { // anonymous function alert( "Did you need something?" ); }; // note: semi-colon required 4 eventViaAnonFunct.html

Validation via onblur() Event Discussion: The onblur() event is triggered when an element loses focus. It is ideal for text box controls because the user has usually completed text entry when the event fires. Example: Age: <input type="text" id="age" onblur="checkNumber( 'age' );" /> IQ: <input type="text" id="iq" onblur="checkNumber( 'iq' );" /> <input type="button" value="Post to Facebook" /> function checkNumber( id ) { var textObj = document.getElementById( id ); var textStr = textObj.value; var regExp = /^\d+$/; // one or more numeric digits if ( ! textStr.match( regExp ) ) { alert( "'" + textStr + "' is not a number." ); } 5 eventOnblurValidation.html

Prompt value via onfocus() and onblur() Discussion: The onfocus() event is triggered when an element gains focus. It is ideal for text box controls when the user is ready to begin entering text. Example: First: <input type="text" id="fname" value="first name only" /> Last: <input type="text" id="lname" value="enter last name" /> function textPrompt( id, prompt ) { var nameField = document.getElementById( id ); nameField.onfocus = function() { if ( nameField.value == prompt ) { nameField.value = ""; // clear prompt } }; nameField.onblur = function() { if ( nameField.value == "") { // no text input nameField.value = prompt; // display prompt } }; } textPrompt( "fname", "first name only" ); textPrompt( "lname", "enter last name" ); 6 eventOnblurOnFocus.html

Image hover via onmouseover() and onmouseout() Discussion: The onmouseover() event is triggered when the mouse enters the boundary of an element. The onmouseout() event is triggered when the mouse leaves the boundary of an element. Example: var imgObj = document.getElementById( "trainLight" ); imgObj.onmouseover = function() { imgObj.setAttribute( "src", "images/lightLeft.png" ); }; imgObj.onmouseout = function() { imgObj.setAttribute( "src", "images/lightRight.png" ); }; 7 hoverOnmouseoverOut.html

Toggle Classes via onclick() (CSS) Discussion: Two different classes can be set and toggled by a clickable div. Example: div#divBlock { height: 100px; width: 300px; font-size: 100px; line-height: 100px; /* vertical alignment */ text-align: center; /* horizontal alignment */ font-weight: bold; padding: 25px; } div.redDiv { background-color: red; } div.greenDiv { background-color: green; } 8 toggleClassesOnclick.html

Toggle Classes onclick() via Chaining Discussion: Multiple methods and properties can be “chained” together. The current class of an element can be retrieved and set via the “className” attribute. Example: Red document.getElementById("divBlock").onclick = function() { if ( document.getElementById("divBlock").className == "redDiv" ) { document.getElementById("divBlock").className = "greenDiv"; document.getElementById("divBlock").innerHTML = "Green"; } else { document.getElementById("divBlock").className = "redDiv"; document.getElementById("divBlock").innerHTML = "Red"; } }; 9 toggleClassesOnclickViaChain.html

Toggle Classes onclick() via Variables Discussion: Instead of using “chaining” variables can be used to define the values. The current class of an element can be retrieved and set via the “className” attribute. Example: Red var toggleBlock = document.getElementById("divBlock"); toggleBlock.onclick = function() { if ( toggleBlock.className == "redDiv" ) { toggleBlock.className = "greenDiv"; toggleBlock.innerHTML = "Green"; } else { toggleBlock.className = "redDiv"; toggleBlock.innerHTML = "Red"; } }; 10 toggleClassesOnclickViaVar.html

Event onkeyup() via text entry Discussion: The onkeyup() event is triggered after any key is released. Example: <textarea rows="5" cols="60" id="textBlock"> "Desire that your life count for something great! John Piper, Don't Waste Your Life Count: <input type="text" id="textCount" size="4" value="193" readonly /> var textObj = document.getElementById( "textBlock" ); var counterObj = document.getElementById( "textCount" ); textObj.onkeyup = function() { counterObj.value = textObj.value.length; }; 11 eventOnkeypressTextCount.html