Beruflich Dokumente
Kultur Dokumente
In the last skill, we have learned the basics of JavaScript language. But, we have never seen the
relation between JavaScript and the web page. Here we are going to start, so in this super skill,
we are going to see:
What’s DOM?
How to access DOM elements?
How to manipulate the DOM elements?What are the events and how to treat them in JavaScript?
How to treat form in JavaScript?
DOM representation
JavaScript is most commonly used to get or modify the content or value of the HTML elements on
the page, as well as to apply some effects like show, hide, animations etc. But, before you can
perform any action we need to find or select the target HTML element.
Here is some definition before we start:
Document interface represents any web page loaded in the browser and serves as an entry point
into the web page's content, which is the DOM tree. The DOM tree includes elements such as
<body> and <table>, among many others. It provides functionality globally to the document, like
how to obtain the page's URL and create new elements in the document.
<!DOCTYPE html>
<html lang="en">
<head>
<script>
console.log(document);
</script>
<title>Document</title>
</head>
<body>
</body>
</html>
What's DOM Attributes?
The attributes are special words used inside the start tag of an HTML element to control the tag's
behavior or provides additional information about the tag.
JavaScript provides several methods for adding, removing or changing an HTML element's
attribute. In the following sections we will learn about these methods in details.
<body>
<script>
// Selecting element
elem.style.fontSize = '18px';
elem.style.fontWeight = 'bold';
console.log('elem:', elem);
</script>
</body>
Add Events
Events refer to what happens to an HTML element, such as clicking, focusing, or loading, to
which we can react with JavaScript.
We can assign JS functions to listen for these events in elements and do something when the
event has occurred.
Here is a list of events. There are three ways you can assign a function to a certain event.
If foo() is a custom function, you can register it as a click event listener (call it when the button
element is clicked) in three ways:
// first way
<button onclick=foo>Alert</button>
<div id="main">
<button>Alert</button>
</div>
<script>
btn.onclick=foo;
</script>
// the third way
<div id="main">
<button >Alert</button>
</div>
<script>
btn.addEventListener('click', foo);
</script>
Form validation
The form validation process typically consists of two parts— the required fields validation which is
performed to make sure that all the mandatory fields are filled in, and the data format validation
which is performed to ensure that the type and format of the data entered in the form is valid.
Well, let's get straight to it and see how this actually works.
HTML
<form name="loginForm" onsubmit="return validateForm(event) ">
<label for="Name">Name:</label>
<label for="password">Password:</label>
<button type="submit">Login</button>
<button type="reset">cancel</button>
</form>
</body>
JavaScript
function validateForm(e) {
e.preventDefault()
if (name.length==0)
if (password.length<5)
DOM RECAP
Congrats !
For the present; we have learned to define the structure and the content using HTML, add to our
structure same design, and manipulate what we have produced using DOM and JavaScript.
See you in the next part of this course.