HTML DOM The Document Object Model is a Javascript class that defines HTML elements as objects. It supports … properties methods events Image from http://www.w3schools.com/js/js_htmldom.asp.

Slides:



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

Bring Life to Your Web Pages with JavaScript and HTML5 Ole Ildsgaard Hougaard -
function coffeeinfo() { document.getElementById('p3').innerHTML = "The word 'coffee' was.
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
JavaScript (Part 2) CS 183 4/13/2010. JavaScript (Part 2) Will cover: ◦ For.. In ◦ Events ◦ Try.. Catch, Throw ◦ Objects.
20-Jun-15 JavaScript and HTML Simple Event Handling.
UNIT 6 JAVASCRIPT EVENT HANDLERS &WEB BROWSERS DETECTION.
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.
Chapter 19: Adding JavaScript
JavaScript II ECT 270 Robin Burke. Outline JavaScript review Processing Syntax Events and event handling Form validation.
JavaScript – part II. The if Statement if (condition) { block of code to be executed if the condition is true }
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.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
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
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.
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.
1 CHAPTER 03: GOING FURTHER By Tharith Sriv. This chapter covers the following topics:  Flying texts or images  Using Comment  Using tag  Using …
C H 07: M ORE A BOUT HTML Tharith Sriv. O UTLINE You have already learnt almost everything in HTML. In this chapter, you will learn more about:  An HTML.
Event Handling © Copyright 2014, Fred McClurg All Rights Reserved.
Flow content – текст и тэги:  a  abbr  address  area (если в тэге map)  article  aside  audio  b  bdi  bdo  blockquote  br  button  canvas.
More on Events Some More Events Image Rollover Objects Select-Option List Control 1.
Chapter 19 Creating and Processing HTML Forms. How HTML Forms Transmit Data name1=value1&name2=value2...&nameN =valueN GET or POST GET, an HTTP GET request,
Thursday, August 6 th, 2015 Instructor: Craig Duckett Event Handling.
DOM Events. JS Events Outline About Events – Introduction – Registering events – Getting information from events Event order/phases – Preventing default.
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.
Project 8: Reacting to Events Essentials for Design JavaScript Level One Michael Brooks.
Basic Webpage Design Mark-up html document with images.
COS 125 DAY 20. Agenda Assignment 8 not corrected yet Assignment 9 posted  Due April 16 New course time line Discussion on Scripts 
 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.
JavaScript Functions A function is a block of code that can be reused. It is similar to a method in Java. It consists of a function name, an argument.
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.
Document Object Model Nasrullah. DOM When a page is loaded,browser creates a Document Object Model of the Page.
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.
Drop-down box. Objectives Learn the HTML syntax of a drop-down list javascript properties of a list/menu: length options selectedIndex The location sub-object.
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.
Week 5.  Normal document flow  Affecting document flow with float and position properties using CSS  Using these properties to create layouts.
Chapter 6 Dynamic Documents with JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Introduction Def: A dynamic HTML document is.
4.1. Manage and maintain JavaScript Update the UI by using JavaScript. Understanding JavaScript and Coding Essentials.
CIS 375—Web App Dev II DHTML. 2 Introduction to DHTML _________ HTML is “a term used by some vendors to describe the combination of HTML, style sheets.
SE-2840 Dr. Mark L. Hornick 1 Dynamic HTML Handling events from DOM objects.
Methods and Object Information. Some Document Methods.
THE DOM.
JavaScript and HTML Simple Event Handling 11-May-18.
JavaScript Print slides by Sean Boyle, with overall content based on material provided by Dan Bogaard, from:
Content (text,image,table)
Week 4: Introduction to Javascript
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
JavaScript and HTML Simple Event Handling 19-Sep-18.
JavaScript Events.
Graphics Considerations
Code Topic 9 Standard JavaScript Events Laura Friedman
JavaScript Examples 27-Apr-19.
JavaScript Examples 30-Apr-19.
JavaScript and Ajax (JavaScript Events)
Web Programming and Design
JavaScript Examples 12-Jul-19.
Web Programming and Design
JavaScript and HTML Simple Event Handling 26-Aug-19.
JavaScript and HTML Simple Event Handling 4-Oct-19.
Presentation transcript:

HTML DOM The Document Object Model is a Javascript class that defines HTML elements as objects. It supports … properties methods events Image from http://www.w3schools.com/js/js_htmldom.asp See http://www.w3schools.com/jsref/dom_obj_document.asp for more on the DOM

DOM Elements Finding elements: document.getElementById(id) document.getElementsByTagName(tag) document.getElementsByClassName(class) Finding HTML objects: document.links document.readyState document.lastModified Changing elements: element.innerHTML = new html content element.attribute = new value element.setAttribute(attribute, value) element.style.property = new property Arguments must be passed as strings See http://www.w3schools.com/js/js_htmldom_document.asp for more HTML objects readyState is either loading, interactive, or complete (returned as a string)

HTML DOM Events There are 3 ways of registering event handlers for a DOM element: HTML Attribute: <button id="mybutton" onclick="alert('Hello world!')"> Event Listener: var myButton = document.getElementById("mybutton"); myButton.addEventListener('click', function(){alert('Hello world');}, false); DOM Element Property: myButton.onclick = function(event){alert('Hello world');}; See https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Events for how to register handlers See http://www.w3schools.com/jsref/dom_obj_event.asp for a list of event handlers

Types of Events Mouse events Keyboard events Form events Media events onclick, ondblclick onmousedown, onmouseup onmouseover, onmouseout Keyboard events onkeydown, onkeypress, onkeyup Form events onfocus, onblur onchange, onselect onsubmit Media events oncanplay, onwaiting, onpause See http://www.w3schools.com/jsref/dom_obj_event.asp for a complete listing

Event Functions Event handlers can accept an argument of this to refer to what was clicked on function changeIt(obj) { obj.innerHTML = "I've changed!"; } … <p onclick="changeIt(this)"> Click on this paragraph to change it </p> Event handlers can also accept an argument of event: to access the event's properties function locateIt(evt) { var xval = evt.clientX; var yval = evt.clientY; alert("Clicked at " + xval + ", " + yval); } … <p onclick="changeIt(this)"> Click somewhere on this paragraph to see a location </p> See https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Events for how to register handlers See http://www.w3schools.com/jsref/dom_obj_event.asp for a list of event handlers

Event Object Properties Event handlers can accept an argument of this to refer to what was clicked on function changeIt(obj) { obj.innerHTML = "I've changed!"; } … <p onclick="changeIt(this)"> Click on this paragraph to change it </p> Event handlers can also accept an argument of event: to access the event's properties function locateIt(evt) { var xval = evt.clientX; var yval = evt.clientY; alert("Clicked at " + xval + ", " + yval); } … <p onclick="changeIt(this)"> Click somewhere on this paragraph to see a location </p> See https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Events for how to register handlers See http://www.w3schools.com/jsref/dom_obj_event.asp for a list of event handlers See http://www.javascriptkit.com/jsref/event.shtml for event properties, and http://www.quirksmode.org/js/events_properties.html for a discussion on how these properties vary among web browsers.

Example: Slideshow 1) Create containers using absolute-positioned container elements <header> <h1>Visions of Maine</h1> </header> <div id="leftscroll"> <button onclick="scrollleft()"> <<</button></div> <div id="rightscroll"> <button onclick="scrollright()"> >></button></div> <div id="imgcontainer"> <img id="myimage"> </div>

Example: Slideshow 2) Style the elements so that they are centered #leftscroll { position: absolute; left: 0px; top: 50%; height: 50px; margin: -25px 0 0 20px; } #rightscroll { right: 0px; top: 50%; height: 50px; margin: -25px 20px 0 0; #imgcontainer { left: 150px; top: 100px; right: 150px; bottom: 30px; img { position: relative; height: 100%; width: auto; margin: 0 auto 0 auto; See https://codemyviews.com/blog/how-to-center-anything-with-css for tips on centering divs

Example: Slideshow 3) Define the images in an array var plist = new Array("img1.jpg", "img2.jpg", "img3.jpg"); var pindex = 0; var plen = plist.length; 4) Define the scrolling functions var myImage = document.getElementById("myimage"); myImage.src = plist[0]; function scrollright() { pindex++; if (pindex >= plen) pindex = 0; myImage.src = plist[pindex]; } function scrollleft() { pindex--; if (pindex < 0) pindex = plen-1;