JS Dom
JS Dom
Page no: 1 of 10
What can JS do with help of DOM?
With the object model, JavaScript gets all the power it needs to
create dynamic HTML:
1. JavaScript can change all the HTML elements in the page
2. JavaScript can change all the HTML attributes in the page
3. JavaScript can change all the CSS styles in the page
4. JavaScript can remove existing HTML elements and
attributes
5. JavaScript can add new HTML elements and attributes
6. JavaScript can react to all existing HTML events in the
page
7. JavaScript can create new HTML events in the page
It defines:
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
Page no: 1 of 10
The getElementById Method: The most common way to access an
HTML element is to use the id of the element. In the example
above the getElementById method used id="demo" to find the
element.
Page no: 1 of 10
b. using document.write() to directly write on output stream
2. JS forms
Form validation
HTML form validation can be performed by JS
It validates if the input in fields of form are according to the rules set
by the receiver
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
<script>
function validateInput() {
let x = document.forms["myform"]["fname"].value;
let y = document.forms["myform"]["roll"].value;
if (x === "") {
alert("Name must be filled");
return false;
} else if (/[0-9!@#$%^&*()_+={}|[\]\\:;"'<>,.?/]/.test(x)) {
alert("Numbers and symbols are not allowed in the name");
return false;
}
if (y === "") {
alert("Roll number is required");
return false;
} else if (y < 20) {
alert("Roll number must be less than 20");
return false;
} else if (/[!@#$%^&*()]/.test(y)) {
alert("Roll number cannot contain special symbols");
return false;
}
}
</script>
</head>
<body>
<h1>Form Validation</h1>
<form name="myform" action="/" onsubmit="return
validateInput()" method="post">
Name: <input type="text" name="fname">
Roll no: <input type="number" name="roll">
<input type="submit" name="submit">
</form>
</body>
</html>
Simply type required in field that must be added in form it will
automatically validate the form
Data Validation
Data validation is the process of ensuring that user input is clean,
correct, and useful.
Typical validation tasks are:
o has the user filled in all required fields?
o has the user entered a valid date?
o has the user entered text in a numeric field?
Validation can be defined by many different methods, and deployed
in many different ways.
Page no: 1 of 10
1. Server side validation is performed by a web server, after input has
been sent to the server.
2. Client side validation is performed by a web browser, before input
is sent to a web server.
Page no: 1 of 10
1. Bubbling:
the inner most element's event is handled first and then the
outer: the <p> element's click event is handled first, then the
<div> element's click event.
2. Capturing:
the outer most element's event is handled first and then the
inner: the <div> element's click event will be handled first, then
the <p> element's click event
With the addEventListener() method you can specify the propagation
type by using the "useCapture" parameter: