Unit 2 - DOM wt
Unit 2 - DOM wt
Assigning Address
of Person to Object
X
JAVASCRIPT DOM Date : 13.09.2024
Value
1
2
3
<!DOCTYPE html>
<html> <script type="text/javascript">
<head> function getAdd() {
<title>DOM manipulation</title> const num1 = Number(document.getElementById("val1").value);
</head> const num2 = Number(document.getElementById("val2").value);
<body> const add = num1 + num2;
<label>Enter Value 1: </label> console.log(add);
<input type="text" id="val1" /> document.getElementById("result").innerHTML = "Addition : " + ad
<br /> document.getElementById("result").style.color = "red";
<br /> }
<label>Enter Value 2: </label> </script>
<input type="text" id="val2" /> </body>
<br /> </html>`
<button onclick="getAdd()">Click To Add</button>
<p id="result"></p>
1
2
3
<style>
</head>
/* unvisited link */ <body>
a:link { <p><b><a href="default.asp" target="_blank">This is a link</a></b></p>
color: red; </body>
} </html>
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
</style>
ACTIVITY – 1
HOT – QUESTION
Padding : 20px
<!DOCTYPE html>
<html>
<head> </head>
<style> <body>
/* This will change the color of the first paragraph in the body */
body > p:first-child { <p>This is some text. (First child of body)</p>
color: blue; <p>This is some text. (Second child of body)</p>
}
<div>
/* This will change the color of the first paragraph inside the div */ <p>This is some text. (First child of div)</p>
div > p:first-child { <p>This is some text. (Second child of div)</p>
color: green; </div>
}
</body>
/* This will change the color of the second paragraph inside the div */ </html>
div > p:nth-child(2) {
color: orange;
}
</style>
JavaScript HTML DOM Events
Reacting to Events onclick=JavaScript
1.<html>
2.<head>
3.<title>
4.Changes current HTML element
5.</title>
6.</head>
7.<body>
8.<center>
9.<h1 onclick="this.style.color='blue'"> This is a JavaTpoint Sit
e </h1>
10.<center>
11.</body>
12.</html>
1.<html>
2.<head>
3. <title>
4.changes the particular HTML element example
5.</title>
6.</head>
7.<body>
8. <p id="demo"> This text changes color when click on the following different buttons. </p>
9. <button onclick="change_Color('green');"> Green </button>
10. <button onclick="change_Color('blue');"> Blue </button>
11.<script type="text/javascript">
12.function change_Color(newColor) {
13. var element = document.getElementById('demo').style.color = newColor; }
14.</script>
15.</body>
16.</html>
DHTML
Example of events: Events
1.Click a button.
2.Submitting a form.
3.An image loading or a web page loading, etc.
1.<html>
2.<head>
3.<title>
4.Example of onclick event
5.</title>
6.<script type="text/javascript">
7.function ChangeText(ctext)
8.{
9.ctext.innerHTML=" Hi JavaTpoint! ";
10.}
11.</script>
12.</head>
13.<body>
14.<font color="red"> Click on the Given text for changing it: <b
r>
15.</font>
16.<font color="blue">
17.<h1 onclick="ChangeText(this)"> Hello World! </h1>
18.</font>
19.</body>
20.</html>
1.<html>
2.<head>
3.<title>
4.Example of onsubmit event
5.</title>
6.</head>
7.<body>
8.<form onsubmit="Submit_Form()">
9.<label> Enter your name: </label>
10.<input type="text">
11.<label> Enter your Roll no: </label>
12.<input type="Number">
13.<input type="submit" value="submit">
14.</form>
15.<script type="text/javascript">
16.function Submit_Form()
17.{
18.alert(" Your form is submitted");
19.}
20.</script>
21.</body>
22.</html>
DHTML
1.<html> DOM
2.<head>
3. <title>
4.Example of DHTML DOM
5.</title>
6.</head>
7.<body>
8.<font color = "blue">
9. <p id="demo"> This text changes color when the page loaded
. </p>
10.</font>
11.<script type="text/javascript">
12.document.getElementById('demo').style.color = "red";
13.</script>
14.</body>
15.</html>
JSON - Introduction
JSON stands for JavaScript Object Notation