The document discusses JavaScript and the Document Object Model (DOM). It introduces JavaScript and how it can access and modify elements on an HTML page using the DOM. The DOM represents the page as a tree structure and JavaScript can select elements using methods like getElementById and getElementsByTagName. Examples are given to demonstrate how to access and update HTML elements via their ID and tag name properties.
3. JS- Java Script
Java Script
Client Side
Scripting
Language
Dynamic front-end
scripting
language
4. JavaScript Advantages
Implementing form
validation
•Changing an image
on moving mouse over
it
•Content loading and
changing dynamically
• Sections of a page
appearing and
disappearing
• React to user actions,
• Performing complex
calculations
5. What Can JavaScript Do?
• Can validate form
data
• Can read and write
HTML elements
• Can access / modify
browser cookies
Can handle events
6. What Can JavaScript can’t Do?
• Can not close a window
that it hasn't opened.
•Cannot Protect Your
Page Source or Images
• Can not do database related
operation
Cannot Read From or Write
to Files in the Client.
7. JavaScript - Placement in HTML File
Script in
<body>...</body> section
Script in External file and then include
link in HTML file head section
Script in
<head>...</head> section
Script in
<head> and <body> section
8. Syntax of Javascript
Write JavaScript code inside HTML
Head Tag, Body Tag or External File
Use <Script> tag inside Head,body tag
Example:
<script >
JavaScript code
</script>
9. Java Script Example- 1
Print Hello Message
<html>
<body>
<script>
document.write("Hello World")
</script>
</body>
</html>
Script Code in HTML
file in Body Tag
10. Java Script Example- 2
Print Hello Message
<html>
<head>
<script>
document.write("Hello World")
</script>
</head>
</html>
Script Code in HTML
file in Head Tag
16. JS- Java Script Variables
Example By using Var
var x = 5;
var y = 6;
var z = x + y;
Example By using let
let x = 5;
let y = 6;
let z = x + y;
Example By using Const
const x = 5;
const y = 6;
let z = x + y;
Example By using nothing
x = 5;
y = 6;
z = x + y;
17. Variable naming conventions in
JavaScript
● You should not use any of the JavaScript reserved keywords as a variable name.
These keywords are mentioned in the next section. For example, break or boolean
variable names are not valid.
● JavaScript variable names should not start with a numeral (0-9). They must begin
with a letter or an underscore character. For example, 123test is an invalid variable
name but _123test is a valid one.
● JavaScript variable names are case-sensitive. For example, Name and name are
two different variables.
19. Java Script Data Types
Java Script Data
Types
Primitive Non-Primitive
20. Java Script Data Types
Primitive
Data Type Description
String represents sequence of characters e.g. "hello"
Number represents numeric values e.g. 100
Boolean represents boolean value either false or true
Undefined represents undefined value
Null represents null i.e. no value at all
21. Java Script Data Types
Non- Primitive
Data Type Description
Object represents instance through which we can access members
Array represents group of similar values
RegExp represents regular expression
48. JavaScript -Arrays
❏ How to Create array in Javascript
❏ How to access Values of Arrays in Javascript
❏ Arrays Properties and Methods
49. Java Script -Arrays
How to Create array in Javascript
● Method 1:
○ var cars = ["Saab", "Volvo", "BMW"];
● Method 2:
○ var cars = new Array("Saab", "Volvo", "BMW");
● Method 3:
○ cars[0] = “Saab”
○ cars[1]= “Volvo”
○ cars[2]= “BMW”
50. Java Script -Arrays
How to access Values of Arrays in Javascript
● Method 1:- To Access only one value
○ var name = cars[0];
● Method 2: To Access only one value with showing it in HTML Page
○ document.getElementById("t1").value = cars[0];
● Method 3: To Access full Array
○ document.getElementById("t1").value= cars;
64. Advantages of JavaScript
❏ Speed: Client-side JavaScript is very fast because it can be run immediately
within the client-side browser.
❏ Simplicity: JavaScript is relatively simple to learn and implement.
❏ Popularity: JavaScript is used everywhere on the web.
❏ Interoperability: JavaScript plays nicely with other languages and can be used
in a huge variety of applications.
❏ Server Load: Being client-side reduces the demand on the website server.
❏ Rich Interface: Gives the ability to create rich interfaces.
65. Disadvantages of JavaScript
❏ Client-Side Security: Because the code executes on the users’ computer, in some
cases it can be exploited for malicious purposes. This is one reason some people
choose to disable Javascript.
❏ Browser Support: JavaScript is sometimes interpreted differently by different
browsers. This makes it somewhat difficult to write cross-browser code
68. DOM- Introduction
❏ JavaScript can access all the elements in a webpage making use of
Document Object Model (DOM).
❏ The web browser creates a DOM of the webpage when the page is
loaded.
❏ The DOM defines a standard for accessing documents
❏ The DOM model is created as a tree of objects
69. DOM- Uses
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
71. DOM Levels
DOM Levels
Level 0 Level 1 Level 2
Low Level
Interface
Core,
HTML
Core2, View,
Event, Style,
Traversal, Range
Level 3
CORE3, LOAD and SAVE,
VALIDATION, EVENTS,
and XPATH.
72. DOM- Properties & Methods
Methods Description
getElementById(id) Find an element by element id
getElementsByTagName(name) Find an element by Tag Name
getElementsByClassName(name) Find an element by Class Name
getElementsByName(name) Find an element by element name
write() Writes new text to a document
76. DOM- Examples:
getElementsByClassName
<html>
<head><script>
function f1() {
document.getElementsByClassName("a1")[1].innerHTML="HTML"; }
</script></head>
<body>
<p> Hello</p>
<p class= “a1”> How Are You </p>
<h1 class= “a1”>Good Day </h1>
<input type=button value="click" onclick="f1()">
</body>
</html>
77. DOM- Examples: To Count Total elements
<script>
function f1() {
alert(document.getElementsByTagName("p").length); }
</script><body>
<p> Hello</p>
<p class= “a1”> How Are You </p>
<h1 class= “a1”>Good Day </h1>
<input type=button value="click" onclick="f1()">
</body>
78. DOM- Examples: To change CSS Property
<html>
<head><script>
function f1() {
document.getElementsByClassName("a1")[1].style.color="red"; }
</script></head>
<body>
<p> Hello</p>
<p class= “a1”> How Are You </p>
<h1 class= “a1”>Good Day </h1>
<input type=button value="click" onclick="f1()">
</body>
</html>
79. DOM- Examples:
To Disable/Enable element
<script>
function f1() {
document.getElementsById("t1").disabled= true; }
</script>
<body>
<input type="text" id="t1" >
<input type=button value="click" onclick="f1()">
</body>
88. jQuery Example: To hide Paragraph
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script> </head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
</body>
89. jQuery Example: To change CSS Property
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).css("background-color","Red");
});
});
</script> </head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
</body>
90. jQuery Example: To append/Add element
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).append("Hello");
});
});
</script> </head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
</body>
91. jQuery Example: To remove element
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).remove();
});
});
</script> </head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
</body>