Day-9 DOM-1
Day-9 DOM-1
Day-9 DOM-1
Day-9 : DOM-1
What is DOM?
Imagine this 🤔 you are watching TV and you don't like the show that's being
streamed, and you want to change it and you also want to increase its volume.
To do that, there has to be a way for you to interact with your television. So how
do you control your tv now?
By using remote
The remote serves as the bridge which allows you interact with your television.
In the same way, JavaScript helps you to interact with the HTML page via DOM.
For example, say that you want a button to change colours when it gets clicked
or you want text to be changed. First, you need to reference those elements
from your JavaScript.
The DOM is a tree-like representation of the web page that gets loaded into the
browser.
Day-9 : DOM-1 1
Whenever html document is loaded on browser, corresponding to that document
another representation of document is created which is known as DOM.
<!DOCTYPE html>
<html>
<head>
<title>My Text</title>
</head>
<body>
<h1>My Header</h1>
<p>My Paragraph</p>
</body>
</html>
If you open this in browser the web browser creates a DOM of the webpage
when the page is loaded. The DOM model is created as a tree of objects like this
HTML meets 🤝 JS
Day-9 : DOM-1 2
How to add JS inside HTML
<html>
<head>
<title>Connecting JS</title>
</head>
<body>
</body>
</html>
<script>
function doSomething(){
// Your Code Goes Here
}
</script>
Console.log():
console.log() in javascript is your best friend life long
Day-9 : DOM-1 3
Where to find your best friend ?
As you already know, you should use console inside your js code, so I am writing
console.log() in script tag
<!DOCTYPE html>
<html>
<head>
<title>My Text</title>
</head>
<body>
<h1>My Header</h1>
<p>My Paragraph</p>
</body>
</html>
<script>
console.log("Oh my Friend")
</script>
Day-9 : DOM-1 4
Now with help of your friend let’s see what is hiding in our document
<!DOCTYPE html>
<html>
<head>
<title>My Text</title>
</head>
<body>
Day-9 : DOM-1 5
<h1>My Header</h1>
<p>My Paragraph</p>
</body>
</html>
<script>
console.log(document)
</script>
Output
Write the following out in any page and see what the response is
// URL of a page
console.log(document.URL)
// title of a page
console.log(document.title)
// domain
console.log(document.domain)
// doctype
console.log(document.doctype)
// head
console.log(document.head)
// body
console.log(document.body)
// all elements
console.log(document.all)
// forms
console.log(document.forms)
// links
console.log(document.links)
// images
console.log(document.images)
Day-9 : DOM-1 6
onClick Event:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e44c0603-071
2-47f1-bf04-792c0c66818c/OEAO.mp4
<element onclick="functionToExecute()">Click</element>
Example:
<html>
<head>
<title>OnClick</title>
</head>
<body>
<button onClick="likeMe()">Like👍</button>
</body>
</html>
<script>
function likeMe() {
console.log("Someone liked me")
Day-9 : DOM-1 7
}
</script>
Now when we click on “Like” button, we will get “Someone liked me” in console
Alert():
The alert() method in JavaScript is used to display a virtual alert box.
Day-9 : DOM-1 8
It is mostly used to give a warning message to the users. It displays an alert
dialog box that consists of some specified message (which is optional) and an
OK button.
When the dialog box pops up, we have to click "OK" to proceed.
Syntax
alert(message)
<html>
<head>
<title>OnClick</title>
</head>
<body>
<button onClick="likeMe()">Like👍</button>
</body>
</html>
<script>
function likeMe() {
alert("Someone liked me")
}
</script>
Day-9 : DOM-1 9
Output
Document.getElementById()
Syntax
document.getElementById(id_Name)
Parameters
id The ID of the element to locate. The ID is case-sensitive string which is
unique within the document; only one element may have any given ID.
Return value
An Element object describing the DOM element object matching the specified ID,
or null if no matching element was found in the document.
Example:
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p id="para">I am paragraph</p>
<div id="box">I am box</div>
<h1 id="head-1">I am heading 1</h1>
</body>
Day-9 : DOM-1 10
</html>
<script>
console.log(document.getElementById("para"))
console.log(document.getElementById("box"))
console.log(document.getElementById("head-1"))
</script>
Output:
Day-9 : DOM-1 11
The content inside opening and closing tag is called as innerText also called as
textContent
Syntax
element.innerText
Example:
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p id="para">I am paragraph</p>
<div id="box">I am box</div>
<h1 id="head-1">I am heading 1</h1>
</body>
</html>
<script>
console.log(document.getElementById("para").innerText)
console.log(document.getElementById("box").innerText)
console.log(document.getElementById("head-1").innerText)
</script>
Output:
Day-9 : DOM-1 12
innerText vs innerHTML
innerText innerHTML
Retrieves and sets the content in plain Retrieves and sets the content in HTML
text. format.
Example:
<i> tag is italic tag
<!DOCTYPE html>
<html lang="en">
<head>
<title>innerText vs innerHTML</title>
</head>
<body>
<div id="box">Welcome to <i> Masai School </i></div>
</body>
Day-9 : DOM-1 13
</html>
<script>
console.log(document.getElementById("box").innerText)
console.log(document.getElementById("box").innerHTML)
</script>
Output:
Day-9 : DOM-1 14
Syntax:
element.textContent = "new_value" or
element.innerText = "new_value"
Example:
<html>
<body>
<h1 id="head">Heading 1</h1>
</body>
<script>
// change the text content in the heading
document.getElementById("head").textContent = "H1";
</script>
</html>
Example 2 : Codepen
Value:
Getting values from Input tags
In HTML introduction we have seen so many input tags.
Day-9 : DOM-1 15
To interact with user, we need to get the value of
Full name
Password
For getting those values we have an attribute in input tag known as value
Output:
Day-9 : DOM-1 16
So, whatever we type in that input box, we can capture that in value attribute.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<input id="name" type="text" value="" />
<button onClick="catchValue()">Submit</button>
</body>
</html>
<script>
function catchValue() {
var studentName = document.getElementById("name").value;
console.log(studentName);
}
</script>
Output:
Day-9 : DOM-1 17
Live code : Codepen
Example 2 : Codepen
Let’s take above example of taking input value of name from the user, instead of
consoling the name , let’s try to print name on browser.
For that let’s create <h1> tag in the body and we will try to fill in innerText
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<input id="name" type="text" value="" />
<button onClick="catchValue()">Submit</button>
<!-- Show output here -->
<h1 id="output"></h1>
</body>
</html>
<script>
function catchValue() {
var studentName = document.getElementById("name").value;
document.getElementById("output").innerText = studentName;
}
</script>
Output:
Day-9 : DOM-1 18
Getting values from Select tags
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<select id="gender">
<option value="">Select gender</option>
<option value="MALE">Male</option>
<option value="FEMALE">Female</option>
</select>
<button onClick="getGender()">Submit</button>
</body>
</html>
<script>
function getGender() {
var gen = document.getElementById("gender").value;
console.log(gen);
}
</script>
Day-9 : DOM-1 19