HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1.

Slides:



Advertisements
Similar presentations
Chapter 08: Adding Adding Interactivity Interactivity With With Behaviors Behaviors By Bill Bennett Associate Professor MSJC CIS MVC.
Advertisements

Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
FORMs Lesson TBE 540. Prerequisites Before beginning this lesson, the learner must be able to… –Create basic web pages using a text editor and/or a web.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Georgia Institute of Technology JavaScript part 2 Barb Ericson Georgia Institute of Technology May 2006.
Forms cs105. More Interactive web-pages So far what we have seen was to present the information. (Ex: tables. Lists,….). But it is not enough. We want.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
JavaScript Forms Form Validation Cookies CGI Programs.
20-Jun-15 JavaScript and HTML Simple Event Handling.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.
JavaScript Client Side scripting. Client-side Scripts Client-side scripts, which run on the user’s workstation can be used to: Validate user inputs entered.
Client side programming with JavaScript and DHTML Dr Jim Briggs.
CST JavaScript Validating Form Data with JavaScript.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
CP476 Internet Computing JavaScript and HTML1 1.JavaScript Execution Environment The JavaScript Window object represents the window in which the browser.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript forms.
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.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Event and Event Handling.
JavaScript - Document Object Model. Bhawna Mallick 2 Unit Plan What is DOM and its use? Hierarchy of DOM objects. Reflection of these objects in an HTML.
Introduction to JavaScript. JavaScript Facts A scripting language - not compiled but interpreted line by line at run-time. Platform independent. It is.
1 Forms A form is the usual way that information is gotten from a browser to a server –HTML has tags to create a collection of objects that implement this.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine how events are useful in JavaScript Discover where.
Chapter 19: Adding JavaScript
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: JavaScript & Forms.
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
Chapter 5 JavaScript and HTML Documents. © 2006 Pearson Addison-Wesley. All rights reserved JavaScript Execution Environment - The JavaScript.
CS346 Javascript-7A Events1 DOM Document Object Model and Events.
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.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
CO1552 Web Application Development HTML Forms, Events and an introduction to JavaScript.
JavaScript - A Web Script Language Fred Durao
Intro to JavaScript Scripting language –ECMAScript compliant –Client side vs. server side Syntactic rules –White space –Statement end: ; optional –Comments:
Lecture 10 JavaScript: DOM and Dynamic HTML Boriana Koleva Room: C54
Scott Marino MSMIS Summer Session Web Site Design and Authoring Session 8 Scott Marino.
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.
More on Events Some More Events Image Rollover Objects Select-Option List Control 1.
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
Jaana Holvikivi 1 Introduction to Javascript Jaana Holvikivi Metropolia.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
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.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
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.
7. JavaScript Events. 2 Motto: Do you think I can listen all day to such stuff? –Lewis Carroll.
JavaScript II ECT 270 Robin Burke. Outline Functions Events and event handling Form validation.
JavaScript Events.
JavaScript Event Handlers. Introduction An event handler executes a segment of a code based on certain events occurring within the application, such as.
LESSON : EVENTS AND FORM VALIDATION -JAVASCRIPT. EVENTS CLICK.
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.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Methods and Object Information. Some Document Methods.
JavaScript and HTML Simple Event Handling 11-May-18.
Forms Web Design Ms. Olifer.
Web Development & Design Foundations with HTML5 7th Edition
JAVASCRIPTS AND HTML DOCUMENTS
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript and Forms Kevin Harville.
JavaScript and Ajax (JavaScript Events)
JAVA SCRIPT OBJECTS & DOM
Barb Ericson Georgia Institute of Technology May 2006
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

HTML Forms/Events (Instructor Lesson) The Event model HTML Forms Custom Events 1

Events How do we connect our code with user events on a web page? – e.g. user clicks a button JavaScript doesn't have events BOM objects do 2

3 HTML forms HTML Forms allow us to interact with the user – Instead of prompting we can collect information from the user – The information can be processing on the client- side (on the PC) or sent to server for processing (and storage)

4 GUI Interface By using a computer you've become used to some standard elements – Buttons – Drop down list boxes – Radio buttons and checkboxes – Edit boxes where you can key information Most GUI elements can be easily coded to appear on an HTML form

5 Elements and Events We can tie elements (like Buttons) to JavaScript code We will be using simple elements for now – not ActiveX, Java Applets, or plug-ins

6 Inside the Form All of these GUI elements must be placed inside the HTML form Form tag – – Groups the elements There can be multiple forms on a single HTML page, but only one form is submitted to a server at a time

7 Form Object The creates a form object on the BOM – Has attributes like ACTION - determines where the form is submitted METHOD - determines how the information is submitted – Only needed if we are going to send information to a server

8 Example (using BOM) Can access the form by var myForm = document.myForm; var myForm = document.forms[0];

Accessing an Element in the Form var myForm = document.myForm; Var myFirstname = myForm.firstname; Var myFirstnameValue = myFirstname.value; 9

10 Example (using DOM) Can access the form by var myForm = document.getElementById('myform');

Accessing an Element in the Form var myFirstname = document.getElementById('firstname'); var myFirstnameValue = myFirstname.value; 11

Changing HTML for an Element innerHTML property – Represents everything that is between a beginning tag and an ending tag – E.g. Hi There var myPara = document. getElementById('mypara'); var myHTML = myPara.innerHTML; myHTML has a value of: Hi There 12

Changing innerHTML Hi There var myDiv= document. getElementById('mydiv'); myDiv.innerHTML = " How are you? "; The div would now be set to the following HTML… How are you? 13

Other Form Elements Button Submit Button Radiobutton Checkbox Select-Option List 14

Button ……………………………………………………………….. function someFunction() { var myFirst = document.getElementById("firstname").value; alert(myFirst); } 15

onClick Event The onClick() listener was attached to the Button element object The startIt() function (the handler) was called when the button was clicked by the user 16

Exercise 4.1 Create an HTML Form that has text fields for a user's first name and age Add a button that calls a function The function should… – Retrieve the user entered values – Display the following on the Web page depending on age entered If age is less then 40: You're young. Stay awake! If age is 40 or more: Time to wear a black armband :-( – Note: Use the innerHTML property to display the message 17

18 Submit Button The Submit button allows us to submit the information to the server (we'll discuss in a moment) – onSubmit( ) event handler

19 Using Return Values Remember that a link action ( tag) would be cancelled if a JavaScript function returned false (with the onclick event) Likewise with the Submit button; the onSubmit event and thus the submit( ) method would be cancelled This is a good place to do form validation – is the data valid?

Example 20 function someFuction() { //some code return false; //prevent submit from triggering } <input type="submit" value="ClickMe" onclick="return someFunction();">

Submitting From A Function You can submit a form directly from a JavaScript function Example Form: 21

Example 22 function submitIt() { var myName = document.getElementById("myfirst").value; var myForm = document.getElementById("myform"); myForm.submit(); }

Exercise 4.2 Change the button on the program you wrote in the last exercise to be a submit-button. Change the form tag to …. Make sure that when the user clicks the button the function is called and they don't see the CNN website. 23

Event Handler We may want to make a menu pop-up when the user click on our page We need an event handler – intercepts the event – executes code – format: on e.g onclick, onload 24

Event Handlers as Attributes A 'onclick' event is implicitly recognized in a link Click Me 25

Specifying Events <a href="somepage.htm" name="linkSomePage" onclick="alert('You Clicked?');" > Click Me 26

Calling a Function function linkPage( ) { alert ('You clicked'); return true; } <a href="somepage.htm" name="linkSomePage" onclick="return linkPage( );" > Click Me 27

Return Values in Events If a function returns true the code happens – e.g. the link takes you to another page If the function returns false – e.g. the link does NOT do anything There are exceptions, so test your code! 28

onload( ) function Events for window objects are captured in the tag – 29

RadionButtons Yes No ……………………………………………………………….. function someFunction() { var myRByes = document.getElementById("rbyes"); if (myRByes.checked) { … } } 30

Exercise 4.3 Make a form that looks like this After user select a radio button and clicks the button place a message to the right of the button that says what they clicked. 31

CheckBoxes Option 1 Option 2 ……………………………………………………………….. function someFunction() { var myCB1 = document.getElementById("cb1").value; if (myCB1.checked) { … } } 32

Exercise 4.4 Make a form that looks like this Here are the possible results after clicking… 33

Select-Option List Red White Blue ……………………………………………………………….. function() { var mySelect = document.getElementById("myselect").value; } 34

TextArea The tag handles mulitline text Example… This is line 1 This is line 2 var myTarea = document.getElementById('mytarea'); 35

Textarea - continued The textarea does NOT process HTML code The text in a textarea behaves live it would in a plain text editor (i.e. Notepad) – newlines instead of – no font styles on individual words within textarea You can apply a style to the entire 36

Getting data from a Textarea This is line 1 This is line 2 var myTarea = document.getElementById('mytarea'); alert(myTarea.value); 37

Example Events clicking a link – onClick, onDblClick moving a mouse pointer over some text – onMouseMove, onMouseDown, onMouseUp, onMouseOver Starting or Exiting the page – onLoad, onUnload 38

More Events Changing Focus – onBlur, onFocus Entering Text – onKeyPress, onKeyDown, onKeyUp Choosing an item from a Select-Option list – onChange, onSelect, onBlur, onFocus These will be covered in the Next Lesson! 39

End 40