Removing events Stopping an event’s normal behavior

Slides:



Advertisements
Similar presentations
23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
Advertisements

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.
lyoungblood ******** This is a standard login screen to gain access to the NoteShare program. Click the “OK” button to continue.
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
Modifying existing content Adding/Removing content on a page using jQuery.
In this lecture, you will learn: ❑ How to link between pages of your site ❑ How to link to other sites ❑ How to structure the folders on your web site.
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.
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Hyperlinks in HTML How to “throw an anchor”, or to create a hyperlink in raw HTML.
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.
Working with Numbers parseInt() and parseFloat() Math.round() Other Math object functions isNaN() toFixed()
INTRODUCTION TO WEB DEVELOPMENT AND HTML Lecture 07: Forms - Spring 2011.
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.
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. –
HTML Essentials HyperText. Why HyperText ? Hypertext is text or pictures which reference other pages which the reader can immediately access Hypertext.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
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.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
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.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Unit 10 – JavaScript Validation Instructor: Brent Presley.
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.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript: Conditionals contd.
JQuery.
HTML Again GUI Items Originally HTML 4 Copyright © Curt Hill.
Quick Info Tables: Unsubmitted Transactions Transactions with Alerts
Internal Style Sheets External Style Sheets
Moving away from alert() Using innerHTML Using an empty div section
SurveyDIG 2.1 Tutorial.
Event Handling Mimi Opkins CECS 493 Fall 2016.
Loops BIS1523 – Lecture 10.
Introduction to JavaScript Events
Fun ‘n Games with Selectors
Tutorial 6 Topic: jQuery and jQuery Mobile Li Xu
Retrieving information from forms
Arrays and files BIS1523 – Lecture 15.
Intro to PHP & Variables
HTML Forms and User Input
Unit 27 - Web Server Scripting
Number and String Operations
JavaScript – Part I Mendelsohn.
Word offers a number of features to help you streamline the formatting of documents. In this chapter, you will learn how to use predesigned building blocks.
Easy Chair Online Conference Submission, Tracking and Distribution Process: Getting Started + Information for Reviewers AMS World Marketing Congress /
SEEM4570 Tutorial 5: jQuery + jQuery Mobile
Using Templates and Library Items
JavaScript and Ajax (JavaScript Events)
ECU Foundation Xtender Application
CMSC 202 Exceptions.
Events Part I Mouse Events.
Adding/Removing content on a page using jQuery
Selectors.
Events Part III The event object.
Reading information from files
Modifying HTML attributes and CSS values
Random Stuff Colors Sizes CSS Shortcuts.
Working with Numbers parseInt() and parseFloat() Math.round()
JavaScript Part 2 Organizing JavaScript Code into Functions
Internal Style Sheets External Style Sheets
Retrieving information from forms
Presentation transcript:

Removing events Stopping an event’s normal behavior The Event Object, Part 2 Removing events Stopping an event’s normal behavior

Learning Objectives By the end of this lecture, you should be able to: Understand what is meant by an event’s default behavior and how to prevent it from occuring. Tell events whose behavior you have prevented to return to their default behavior

Stopping the typical/normal behavior of an event Some events, such as clicking on a hyperlink have ‘preprogrammed’ functionality as their default behavior. For example, clicking on a hyperlink has the functionality of sending a request to a web server for a document or file. Similarly, clicking on a ‘Submit’ button typically triggers submission of a form to a web server. However, we sometimes do not want the typical behavior to be carried out. A common situation is when the user clicks the ‘Submit’ button on a form but have left out, or improperly entered some key piece(s) of data. In this case, we may wish to prevent the submit button from carrying out its default behavior. There are a couple of ways of preventing an event form carrying out its normal (default) behavior. One of which is to use the statement return false; in your script. However, though we may discuss this statement down the road, we will not use it for now. Instead, I would prefer you use the preventDefault() function. Here is a paraphrase of the example from the API: <a href="http://jquery.com">Some hyperlinked text that should go to the jQuery page.</a> <div id="log"></div> <!–- Log the result in this space... --> <script> $( "a" ).click(function( event ) { //The API prefers to use double-quotes (either is fine) event.preventDefault(); //The ‘preventDefault()’ function $( "#log" ).append( "Default " + event.type + " prevented" + "<br>" ); });</script>

preventDefault() must be invoked with the ‘event’ object Incidentally, you might be tempted to associate the preventDefault() function directly with the selector like so: $('a').preventDefault(); However, this will not work! The preventDefault() function is actually a part of the event object. In other words, you will invoke preventDefault() within the function that is responsible for handling the event. Note how in the code shown here, the preventDefault() function is invoked from inside the click() function. Also note how we use the event object to invoke the function. We do not use the ‘a’ selector. $( "a" ).click(function( event ) { event.preventDefault(); $( "#log" ).append( "Default " + event.type + " prevented" + "<br>" ); });

API docs for function preventDefault() Check out the API docs page for preventDefault() at http://api.jquery.com/event.preventDefault/ You will note that the object ‘event’ precedes the function name. This tells you that the preventDefault() function is invoked using the ‘event’ object.

Restoring the default behavior of an event What if you wish to return to the original behavior of an event? For example, suppose you wanted to take the previous situation where all anchor tags were no longer opening a new page, and return their behavior to their default hyperlink behavior? Is there some kind of function called, say, restoreDefault() ? As it happens, there is not. However, there is a function called unbind(). This function is a little more straight-forward to use than the preventDefault() function, in that we can simply use the selector. To use unbind() we begin by applying the selector and then invoke the function like so: $('selector').unbind(); However, that’s not the entire story. Recall that preventDefault() is associated with a specific event such as a click event, or a mouseover event, or a keypress event, and so on. As a result, when using unbind() we must tell the function exactly which event we wish to unbind. The reason is that it is possible to have multiple events associated with a single selector. Reacall our earlier example in which we prevented the default behavior for anchor tags clicking like so: $('a').click(function( event ) { event.preventDefault(); }); Note that we provide two key pieces of information : The selector (green), and the event (red). To unbind, then, we also need to refer to both of these items. We do so as follows: $('a').unbind('click');

FILE: prevent_restart_default_behavior.htm Example FILE: prevent_restart_default_behavior.htm