Professional Documents
Culture Documents
Web Unit 4 Solved QP
Web Unit 4 Solved QP
1. What is DOM?
The Document Object Model (DOM) is a programming interface for HTML and XML
documents. It represents the page so that programs can change the document structure,
style, and content.
2. What is an event?
An HTML event can be something the browser does, or something a user does.
Here are some examples of HTML events:
An HTML web page has finished loading
An HTML input field was changed
An HTML button was clicked
3. What is XML?
Xml (eXtensible Markup Language) is a mark up language.
XML is designed to store and transport data.
Xml was released in late 90’s. it was created to provide an easy to use and store
self describing data.
XML became a W3C Recommendation on February 10, 1998.
XML is not a replacement for HTML.
XML is designed to be self-descriptive.
XML is designed to carry data, not to display data.
XML tags are not predefined. You must define your own tags.
XML is platform independent and language independent.
4. What is DTD?
XML Document Type Declaration commonly known as DTD is a way to describe
precisely the XML language. DTDs check the validity of, structure and vocabulary of
an XML document against the grammatical rules of the appropriate XML language.
7. What is DHTML?
The combination of HTML plus JavaScript and the DOM is referred to as Dynamic
HTML (DHTML), and an HTML document that contains scripting is called
a dynamic document.
10. How do you access the elements from the html documents?
There are two ways to access the elements from the documents
1. getElementById( )
2. getElementByName( ) methods
1. Explain the document object model in java script? Explain with example. (5-10
MARKS)
The original motivation for the standard DOM was to provide a specification that would
allow Java programs and JavaScript scripts that deal with XHTML documents to be
portable among various browsers.
The DOM is an application programming interface (API) that defines an interface
between XHTML documents and application programs.
It is an abstract model because it must apply to a variety of application programming
languages.
Each language that interfaces with the DOM must define a binding to that interface.
The actual DOM specification consists of a collection of interfaces, including one for
each document tree node type.
They define the objects, methods, and properties that are associated with their
respective node types.
With the DOM, users can write code in programming languages to create documents,
move around in their structures, and change, add, or delete elements and their content.
Documents in the DOM have a treelike structure, but there can be more than one tree in
a document.
Because the DOM is an abstract interface, it does not dictate that documents be
implemented as trees or collections of trees.
Example-2
In the following example, using the DOM method "getElementById" we have
replaced the original text part "GetElementById" with the text "Tutorix" and the result
is displayed in the output.
<html>
<body>
<p id="element">GetElementById</p>
<script>
document.getElementById("element").innerHTML = "Tutorix";
</script>
</body>
</html>
Output
Tutorix
Example 4: Get the element with id="demo" and change its color:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to change the color of this paragraph.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var x = document.getElementById("demo");
x.style.color = "red";
}
</script>
</body>
</html>
OUTPUT:
Before clicking: After clicking on button:
Output:
Before Clicking: After Clicking:
JavaScript | Events:
The change in the state of an object is known as an Event. In html, there are various events
which represents that some activity is performed by the user or by the browser. When
javascript code is included in HTML, js react over these events and allow the execution. This
process of reacting over the events is called Event Handling. Thus, js handles the HTML
events via Event Handlers.
Javascript has events to provide a dynamic interface to a webpage. These events are hooked
to elements in the Document Object Model(DOM
For example, when a user clicks over the browser, add js code, which will execute the task to
be performed on the event.
Click Event:
1) onclick events: This is a mouse event and provokes any logic defined if the user clicks on the
element (button) it is bound to.
Code #1:
<!doctype html>
<html>
2) onmouseover event: This event corresponds to hovering the mouse pointer over the element and its children,
to which it is bound to.
Code #2:
<!doctype html>
<html>
<head>
<script>
function hov()
{
var e = document.getElementById('hover');
e.style.display = 'none';
}
</script>
</head>
<body>
<div id="hover" onmouseover="hov()"
style="background-
color:green;height:200px;width:200px;">
</div>
</body>
</html>
Keyboard Event:
3) onkeyup event: This event is a keyboard event and executes instructions whenever a key is released after
pressing.
Code #2:
<!doctype html>
<html>
<head>
<script>
var a = 0;
var b = 0;
var c = 0;
function changeBackground()
{
var x = document.getElementById('bg');
bg.style.backgroundColor = 'rgb('+a+', '+b+',
'+c+')';
a += 1;
b += a + 1;
c += b + 1;
if (a > 255) a = a - b;
if (b > 255) b = a;
if (c > 255) c = b;
}
</script>
</head>
<body>
<input id="bg" onkeyup="changeBackground()"
placeholder="write something" style="color:#fff">
</body>
</html>
4) onmouseout event: Whenever the mouse cursor leaves the element which handles a mouseout event, a
function associated with it is executed.
Code #4:
<!doctype html>
<html>
<head>
<script>
function out()
{
var e = document.getElementById('hover');
e.style.display = 'none';
}
</script>
</head>
<body>
<div id="hover" onmouseout="out()"
style="background-color:green;height:200px;width:200px;">
</div>
</body>
</html>
1. First, it determines whether the user typed the initial password (in the first input box)
by testing the value of the element against the empty string. If no password has been
typed into the first field, the function calls alert to produce an error message and
returns false.
2. The second test determines whether the two typed passwords are the same. If they
are different, once again the function calls alert to generate an error message and
returns false.
If they are the same, it returns true.
We now consider an example that checks the validity of the form values for a name and phone number
obtained from text boxes. The pattern for matching names [LastName, FirstName, MiddleName] is as
follows:
/^d{3}-\d{8}$/
The following is the XHTML document, validator.html, that displays the text boxes for a customer’s
name and phone number: