JS-DOM-Events
JS-DOM-Events
OBJECT
🞆An object is a thing or entity.
⚫ Browser window
⚫ Submit button
⚫ Web page document
2
DOCUMENT OBJECT MODEL
🞆 The backbone of an HTML document is tags.
🞆 When a web page is loaded, the browser creates a Document Object Model of
the page
🞆 The HTML DOM is a standard for how to get, change, add, or delete
HTML elements.
DOCUMENT OBJECT MODEL (DOM)
⚫ A portion of the DOM is
shown at the left.
⚫ Hierarchical structure
DOM viwer
http://software.hixie.ch/utilities/js/live-dom-viewer/dd text
5
WITH DOM YOU WILL LEARN HOW TO:
🞆How to access HTML elements through JavaScript
🞆document.title
⚫ The title of the HTML document, usually the string that is
displayed in the browser’s window caption or tab bar
🞆document.body
⚫ Represents the body of the document
EXAMPLE
<head>
<title>My Document</title>
</head>
<body>
<script type = "text/JavaScript">
window.alert(document.title);
document.title = "Godzilla's Lair";
window.alert(document.title);
</script>
</body>
READ ONLY DOCUMENT ATTRIBUTES
🞆document.URL
⚫ A string that contains the full pathname of the currently open
HTML document
🞆document.lastModified
⚫ A string that contains the date for which the currently open
HTML document was last modified
DOM ACCESS
🞆 On every webpage, the document
object gives us ways of accessing and
changing the DOM.
<html>
<head>
<title>My Document</title>
</head>
<body>
<p id= " foundation" >Founded in 1991, PSUT is the only private and non-
profit university in Jordan. </p>
<p class= " psut" >Being part of El Hassan Science City, PSUT strives to
create a culture of entrepreneurship among its students.</p>
<p class= " psut" >PSUT encourages creativity and innovation in all areas
of life. </p>
</body>
</html>
GETELEMENTBYID()
<script>
var parag= document.getElementById(“foundation");
document.write(parag.innerHTML);
</script>
GETELEMENTSBYCLASSNAME()
<script>
var paragraphs = document.getElementsByClassName("psut");
</script>
GETELEMENTSBYTAGNAME()
JS
let myVehicle = document.querySelector(“ol li");
alert(myVehicle.innerHTML); // will alert with Bicycle
DOCUMENT.QUERYSELECTOR()
HTML
<h3>vehicle category </h3>
<ol>
<li>Bicycle</li>
<li>Motorcycle</li>
<li>Car</li>
<li>Bus</li>
</ol>
JS
let allVehicles = document.querySelectorAll(“ol li");
for( i=0; i< allVehicles.length; i++)
documrnt.write(allVehicles[i].innerHTML + “<br>”); // will output: Bicycle
Motorcycle
Car
Bus
HTML ELEMENT ATTRIBUTES
🞆All HTML objects have the following attributes, for
which you can change using the HTML DOM
⚫ id
⚫ className
⚫ title
⚫ innerHTML
🞆Inner HTML is the content between the start and end tags of an
HTML element
ACCESSING AN ATTRIBUTE VALUE
🞆getAttribute()
🞆setAttribute()
🞆innerHTML
🞆style
GETATTRIBUTE()
HTML
<img src="stratocaster.jpg" alt="electric guitar" id="lead-image">
JS
HTML
<img src="stratocaster.jpg" alt="electric guitar" id="lead-image">
JS
HTML
<div class=“intro">First Div</div>
<div class=“intro">Second Div</div>
<div class=“intro"><p>Third Div<p></div>
JS
HTML
<div id=“myintro">Intro Div</div>
JS
document.getElementById(“myintro").style.color = "#fff";
document.getElementById(“myintro").style.backgroundColor = "#f58220";
NOT
background-color
as in CSS
CHANGE CSS CLASS USING
GETELEMENTBYID
document.getElementById("myElemnt").className =
"myStyle";
Example:
HTML:
<p id=“intro” class = “basic”>Sample Text</p>
CSS:
.basic{ }
.advanced{}
JavaScript:
document.getElementById(“intro").className = “advanced";
COUNT HOW MANY CHECKBOXES CHECKED
HTML
<form id="vehicleGroup">
<input type="checkbox" name="vehicles" value="car“ /> Car
<input type="checkbox" name="vehicles" value="truck“ /> truck
<input type="checkbox" name="vehicles" value="bike“ /> bike
</form>
JS
var numchecked = 0;
var dom=document.getElementById("vehiclegroup");
for (index=0,index<dom.vehicles.length;index++)
if (dom.vehicles[index].checked)
numchecked++;
alert(numchecked);
ADDING AND REMOVING ELEMENTS
🞆createElement()
🞆createTextNode() =
🞆appendChild()
🞆insertBefore()
🞆replaceChild()
🞆removeChild()
CREATEELEMENT()
🞆enter text into either an element we’ve created or an
existing element on the page
HTML
<body>
…
</body>
JS
JS
JS
newParagraph.appendChild( copy );
ourDiv.appendChild( newParagraph );
INSERTBEFORE()
🞆inserts an element before another element
HTML
<body>
<div id="our-div">
<p id="our-paragraph">Our paragraph text</p>
</div>
</body>
JS
var ourDiv = document.getElementById("our-div");
var para = document.getElementById("our-paragraph");
HTML
<body>
<div id="our-div">
<div id="swap-me"></div>
</div>
</body>
JS
var ourDiv = document.getElementById("our-div");
var swapMe = document.getElementById("swap-me");
JS
var parentDiv = document.getElementById("parent");
var removeMe = document.getElementById("remove-me");
parentDiv.removeChild( removeMe );
JAVASCRIPT AND EVENTS
🞆JavaScript can be configured to perform actions
when events occur.
⚫ The event name is coded as an attribute of an HTML
tag
⚫ The value of the event attribute contains the JavaScript
code
Example:
Display an alert box when the mouse is placed over a
hyperlink.
39
APPLYING EVENTS HANDLERS
40
HANDLERS AS AN HTML ATTRIBUTE
HTML
<body onclick="myFunction();">
….
</body>
JS
function myFunction()
{
alert(“An element clicked");
}
HANDLERS AS A METHOD
HTML
<body>
….
</body>
JS
function myFunction()
{
alert(“An element clicked");
window.onclick = myFunction;
ADDEVENTLISTENER
HTML
<body >
….
</body>
JS
window.addEventListener("click", myFunction);
JS
// with an anonymous function
window.addEventListener("click", function(e) {
alert(“An element clicked");
});
EXAMPLE
<head>
<title>Demo</title>
<script lang = "javascript">
function thankuser() {
window.alert("thanks");
}
</script>
</head>
<body>
<button onclick = "thankuser();">
Click Me!
</button>
</body>
JAVASCRIPT TOOLKITS
45
NOTES FOR JAVASCRIPT DEBUGGING
⚫ Check the syntax of the statements
◦ Pay very close attention to upper and lower case letters, spaces,
and quotations
⚫ Verify that you have saved the page with your most recent
changes
⚫ Verify that you are testing the most recent version of the
page (refresh or reload the page)
🞆 JavaScript Tutorials
http://www.w3schools.com/JS
49
EXERCISE
<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"/>
<title>About Me</title> </head>
<body>
<h1>About Me</h1>
<ul>
<li>Name: <span id="name"></span> </li> <li>Age: <span
id="age"></span> </li>
<li>Hometown: <span id="hometown"></span> </li>
</ul>
</body>
</html>
EXERCISE REQUIREMENT
🞆Add a script tag to the bottom.
🞆Change the body style so it has a font-family of "Arial,
sans-serif".
🞆Replace each of the spans (name, age, hometown) with
your own information.
🞆Iterate through each li and change the class to "listitem".
Add a style tag that sets a rule for "listitem" to make the
color red.
🞆Create a new img element and set its src attribute to a
picture of you. Append that element to the page.
SOLUTION
document.body.style.fontFamily = 'Arial, sans-serif';
document.getElementById('name').innerHTML = 'Pamela Fox';
document.getElementById('age').innerHTML = '27';
document.getElementById('hometown').innerHTML = 'Pasadena, CA';