IS1500: Introduction to Web Development

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

Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
The Web Warrior Guide to Web Design Technologies
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
CST JavaScript Validating Form Data with JavaScript.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
JavaScript Form Validation
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
JavaScript Teppo Räisänen LIIKE/OAMK HTML, CSS, JavaScript HTML defines the structure CSS defines the layout JavaScript is used for scripting It.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
1 CLIENT-SIDE SCRIPTS. Objectives 2 Learn how to reference objects in HTML documents using the HTML DOM and dot syntax Learn how to create client-side.
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.
Integrating JavaScript and HTML5 HTML5 & CSS 7 th Edition.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
HTML Forms.
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
1 JavaScript Functions and Objects. 2 Objectives You will be able to Use JavaScript functions in JavaScript scripts. Get JavaScript functions executed.
Fluency with Information Technology INFO100 and CSE100 Katherine Deibel Katherine Deibel, Fluency in Information Technology1.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 9 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Javascript JavaScript is what is called a client-side scripting language:  a programming language that runs inside an Internet browser (a browser is also.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
This is our seminar JavaScript And DOM This is our seminar JavaScript And DOM.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Introduction to.
Module 1 Introduction to JavaScript
IS1500: Introduction to Web Development
Chapter 5 Validating Form Data with JavaScript
In this session, you will learn to:
Section 17.1 Section 17.2 Add an audio file using HTML
Introduction to JavaScript
Web Programming– UFCFB Lecture 17
Introduction to JavaScript
HTML Forms and User Input
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript Introduction
DHTML Javascript Internet Technology.
A second look at JavaScript
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
DHTML Javascript Internet Technology.
JavaScript Form Validation
Introduction to JavaScript
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Programming with Microsoft Visual Basic 2008 Fourth Edition
Introduction to JavaScript
Web Programming and Design
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

IS1500: Introduction to Web Development JavaScript: Client-Side Forms Martin Schedlbauer, Ph.D. m.schedlbauer@neu.edu

JavaScript: Client-Side Forms Client-Side Scripting with JavaScript Client-Side FORMs IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Client-side forms can add an element of interactivity and form validation without requiring a server-side forms processor. Client-side forms are also faster as they don’t require a round-trip request to a server program. However, client-side forms cannot access data in databases. IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms An Example Daily Rate: <input type="text" id="rate"><br/> Number of Days: <input type="text" id="days"> <p> <button onClick="calculate()">Calculate Fee</button> The charter fee is <div id="fee">XXXX</div> <script> function calculate() { var fee = document.getElementById("rate").value; var days = document.getElementById("days").value; document.getElementById("fee").innerHTML = "$" + (fee * days); } </script> IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Same Example… Here’s the code more “broken up”: … <script> function calculate() { var fee = document.getElementById("rate").value; var days = document.getElementById("days").value; var feeOutput = document.getElementById("fee"); var result = 0; result = fee * days; feeOutput.innerHTML = "$" + result; } </script> These refer to the HTML field element’s ID, NOT the JavaScript variable, even if it has the same name! IS1500 JavaScript: Client-Side Forms

<div> versus <span> Both <div> and <span> allow you to create a referenceable HTML element that you can manipulate from JavaScript or style independently. The difference is that <div> adds a line break before the text while <span> does not. IS1500 JavaScript: Client-Side Forms

Example: <div> vs <span> <p>This is a paragraph of text with a <span style="color:red" id="spanArea">span section</span></p> <p>This is a paragraph of text with a <div style="color:blue" id="divArea">div section</div></p> IS1500 JavaScript: Client-Side Forms

Lack of <form> Section When building a purely client-side form that will not be passed to the server does not require that the form elements be in a <form> section. The <form> tag normally contains the action="" attribute to point to the server-side forms processing program, but this does not apply with client-side forms. It’s nevertheless good coding practice to place the form into a <form> section. IS1500 JavaScript: Client-Side Forms

A JavaScript Calculator For a more elaborate example, study the interactive HTML calculator at: http://www.anaesthetist.com/mnm/javascript/calc.htm IS1500 JavaScript: Client-Side Forms

Client-Side Scripting with JavaScript Image Rollovers & Form Focus with JavaScript Form Focus IS1500 Client-Side Scripting with JavaScript

Client-Side Scripting with JavaScript Setting Field Focus Focus puts the "keyboard focus" towards a particular field, e.g., after an error or to the first field in a form. When a text field is active it is said to be focused. Setting the focus means making the field active so the cursor is blinking and ready to accept text input. IS1500 Client-Side Scripting with JavaScript

Setting Focus on Page Load <!DOCTYPE html> <html> <head> <script> function setFocus() { document.getElementById("fname").focus(); } </script> </head> <body onload="setFocus()"> <form> Name: <input type="text" id="fname" size="30"><br> Age: <input type="text" id="age" size="30"> </form> </body> </html> This example uses the onLoad event of the <body> tab. The event is triggered when the body of the HTML page has loaded. It ensures that the first text field has focus and is ready to accept input without having to “click” on it. IS1500 Client-Side Scripting with JavaScript

JavaScript: Client-Side Forms Client-Side Scripting with JavaScript Getting Form Input IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Text Input Text or free-form number input is accomplished via: <input type="text" id="textField" /> Retrieve the value via: var field = document.getElementById("textField"); var val = field.value; IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Number Input Number input is accomplished via: <input type="number" id="numField" /> Retrieve the value via: var field = document.getElementById("numField"); var val = field.value; <input type="number" id="numGuests" /> <button type="button" onclick="procClick()">Submit</button> <script> function procClick() { var field = document.getElementById("numGuests"); var ng = field.value; alert("Number of guests = " + ng); } </script> IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Checkbox A on/off (true/false) input is accomplished via: <input type="checkbox" id="checkField" /> Retrieve the value via: var field = document.getElementById("checkField"); var val = field.checked; <input type="checkbox" id="isFishing" /> <button type="button" onclick="procClick()">Submit</button> <script> function procClick() { var field = document.getElementById("isFishing"); var fishing = field.checked; if (fishing == true) { alert("fishing"); } else { alert("no fishing"); } </script> IS1500 JavaScript: Client-Side Forms

Option Menus (Dropdowns) A choice from multiple is accomplished via: <option id="dropDown"> Retrieve the value via: var menu = document.getElementById("dropDown"); var val = menu.value; <select id="dropDown" onChange="proc()"> <option>Option 1</option> <option>Option 2</option> </select> <script> function proc() { var menu = document.getElementById("dropDown"); var val = menu.value; alert("selected item = " + val); } </script> IS1500 JavaScript: Client-Side Forms

Triggering with onChange A JavaScript function can be called whenever a value is selected from an option menu rather than waiting for an explicit button press. <select id="dropDown" onChange="proc()"> <option>Option 1</option> <option>Option 2</option> </select> <script> function proc() { var menu = document.getElementById("dropDown"); var val = menu.value; alert("selected item = " + val); } </script> IS1500 JavaScript: Client-Side Forms

Setting Default Values Use the selected attribute to define the default value for a selection menu. <select id="dropDown" onChange="proc()"> <option>Option 1</option> <option selected>Option 2</option> </select> <script> function proc() { // get distance from dropdown (through id value) var menu = document.getElementById("dropDown"); var val = menu.value; alert("selected item = " + val); } </script> IS1500 JavaScript: Client-Side Forms

Retrieving Attributes You can also retrieve the id attribute of the selected item, not just its value. This requires knowing which option item was selected. access this through the selectedIndex property IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms <select id="dropDown" onChange="proc()"> <option id="one">Option 1</option> <option id="two" selected>Option 2</option> </select> <script> function proc() { var menu = document.getElementById("dropDown"); // get the value of the selected item var val = menu.value; // get the id of the selected item var id = menu.options[menu.selectedIndex].id; alert("selected item = " + val + " | " + id); } </script> IS1500 JavaScript: Client-Side Forms

Placing Values into a Text Field The value property of an input field can also be set. <select id="dropDown" onChange="proc()"> <option id="one">Option 1</option> <option id="two" selected>Option 2</option> </select> <input type="text" id="txtField" /> <script> function proc() { var menu = document.getElementById("dropDown"); // get the value of the selected item var val = menu.value; // get the id of the selected item var id = menu.options[menu.selectedIndex].id; // retrieve the input field object var field = document.getElementById("txtField"); // place output into the input field's value field.value = val + " | " + id; } </script> IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Client-Side Scripting with JavaScript Type conversion IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Types JavaScript is dynamically typed and the type of a variable is based on what it holds. However, sometimes you need to force a particular data type. IS1500 JavaScript: Client-Side Forms

Converting Text to Number To convert text to a number: parseInt() parseFloat() <input type="number" id="numDays" /> <button type="button" onclick="procClick()">Submit</button> <script> function procClick() { var field = document.getElementById("numDays"); var days= field.value; // force conversion to a number days = parseInt(days); } </script> IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms What if I don’t Convert? Generally, if a user types a number into an input field, JavaScript recognizes it as a number. However, if you get $NaN when you print out a number you know that JavaScript interpreted the “number” as text and you are required to do a conversion. IS1500 JavaScript: Client-Side Forms

JavaScript: Client-Side Forms Formatting Numbers To format a number to a fixed number of decimals use the toFixed() method: The argument to toFixed(numDigits) is the number of digits after the decimal point. <input type="number" id="numGuests" /> <button type="button" onclick="procClick()">Submit</button> <script> function procClick() { var field = document.getElementById("numGuests"); var ng = field.value; alert("Number of guests = " + ng.toFixed(2)); } </script> IS1500 JavaScript: Client-Side Forms

Converting from Number to Text To force conversion of a number to text is necessary if you need to concatenate a number to text for output. Simply “adding” the number to an empty string forces conversion to text. <input type="number" id="numDays" /> <button type="button" onclick="procClick()">Submit</button> <script> function procClick() { var field = document.getElementById("numDays"); var days= field.value; // force conversion to a number days = parseInt(days); // force conversion to text var text = "" + days; } </script> IS1500 JavaScript: Client-Side Forms

Summary, Review, & Questions… IS1500 JavaScript: Client-Side Forms