Dom V2
Dom V2
DOM
1301236 - WEB-BASED PROGRAMMING
What is Document Object Model
(DOM)?
When a webpage is loaded, the browser
creates a Document Object Model (DOM) of
the page.
The HTML DOM model is constructed as a tree
of Objects:
What is Document Object Model
(DOM)?
The Document Object Model (DOM) specifies how browsers should create a model of an HTML
page and how JavaScript can access and update the contents of a web page while it is in the
browser window.
◦ -getElementByld(‘id’)
◦ Selects an individual element given the value of its id attribute.
◦ The HTML must have an id attribute in order for it to be selectable.
◦ -getElementsByName(‘fname’)
◦ Get all elements with the name “fname”
◦ getElementsByName() returns a collection of elements with a specified name.
Document Object Model (DOM)
Since each tag in a page corresponds to a JavaScript DOM Object, JS code can talk to these
objects to examine elements' state:
◦ e.g. see whether a box is checked
onmouseout="this.style.color='gray'" />
function functionName()
function functionName() {{
// code
// code to
to initialize
initialize the
the page
page
...
...
}}
// run
// run this
this function
function once
once the
the page
page has
has finished
finished loading
loading
window.onload == functionName;
window.onload functionName; //you
//you shouldn't
shouldn't write
write ()() when
when attaching
attaching the
the handler
handler
Onmouseover, onmouseout and onload
Event Handler Example
The example below incorporates onmouseover and onmouseout effects into two buttons.
Onmouseover, onmouseout and onload
Event Handler Example
Another way to use onload event
onmouseover, onmouseout event
example#2
Onchange Event Handler Example
The example below adds onchange effects into a drop down list.
Onchange Event Handler Example
JavaScript
DEALING WITH FORM ELEMENTS
How to get the value of a text field
This example shows how to retrieve the value entered in the text field.
How to get the value of a text field
Getting value from radio button
This example shows how to obtain the value from radio buttons.
Getting value from radio button
Getting value from checkbox
This example shows how to obtain the value from checkbox buttons.
Getting value from checkbox
Using textarea input in JavaScript
Using textarea input in JavaScript
Converter to convert from Fahrenheit to
Celsius, using onclick and JS function
Converter to convert from Fahrenheit to
Celsius, using onclick and JS function
Form Validation Example
The following form validation example is to make sure email addresses are equal.
Form Validation Example
Getting selected value in dropdown list
Getting selected value in dropdown list
JavaScript
DATE OBJECT
JavaScript Date Object
Date objects are created with the new Date() constructor.
new Date() creates a date object with the current date and time.
Example:
Date Get Methods
Method Description
getFullYear() Get year as a four digit number
(yyyy)
getMonth() Get month as a number (0-11)
getDate() Get day as a number (1-31)
getDay() Get weekday as a number (0-6)
getHours() Get hour (0-23)
getMinutes() Get minute (0-59)
getSeconds() Get second (0-59)
getMilliseconds() Get millisecond (0-999)
getTime() Get time (milliseconds since
January 1, 1970)
Date Object Example
JavaScript
TIMING EVENTS
JavaScript Timing Events
JavaScript can be executed in time-intervals, this is called timing events.
The two key methods to use with JavaScript are:
setTimeout(function, Time in milliseconds)
Executes a function, after waiting a specified number of milliseconds.
The clearTimeout() method stops the execution of the function specified in setTimeout().
myVar = setTimeout(function, milliseconds);
clearTimeout(myVar);
setTimeout() Method Example
setInterval() Method Example
JavaScript
STRINGS
Some of JavaScript String Methods
length concat()
substring() trim()
substr() charAt()
toUpperCase() charCodeAt()
toLowerCase() split()
indexOf() lastIndexOf()
Extracting String Parts
substring(start, end): extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: start position, and end position (end not included).
If you omit the second parameter, the method will slice out the rest of the string
substr(start, length): is similar to substring, the difference is that the second parameter
specifies the length of the extracted part.
Example Extracting String Parts
<html>
<body>
<h1>JavaScript String Methods</h1>
<p id="str"></p>
<script>
var str = "Good Morning!";
document.getElementById("str").innerHTML = str.substring(0,6);
</script>
</body>
</html>
Converting to Upper Case
A string is converted to upper case with toUpperCase().
A string is converted to lower case with toLowerCase().
The length property returns the length of a string.
Example Converting to Upper
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="greeting">Convert string to upper case</p>
<script>
function myFunction()
{
var text = document.getElementById("greeting").innerHTML;
document.getElementById("greeting").innerHTML = text.toUpperCase();
}
</script>
</body>
</html>
JavaScript String concat()
The charCodeAt() method returns the unicode of the character at a specified index in a string.
Example:
<script>
var s = "ZEBRA";
var s2 = "AbCdEfG";
document.writeln( "<br >Character code at index 0 in '"+ s + "' is " + s.charCodeAt( 0 ) + "</p>" );
for(var i=0;i<s.length;i++)
</script>
More String Methods
A string can be converted to an array with the split() method.
The trim() method removes whitespace from both sides of a string.
The indexOf() method returns the index (position) the first occurrence of a string in a string. If
a substring is NOT found, then indexOf() will return -1
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string