Front End Web Technologies-Javascript
Front End Web Technologies-Javascript
<!DOCTYPE html>
<body>
<script language ="javascript" type ="text/javascript">
document.write("Hello World!")
</script>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html lang="en">
<body>
<script type = "text/javascript">
function sayHello()
{
alert("Hello World")
}
</script>
<input type ="button" onclick ="sayHello()" value ="Say Hello"/>
</body> </html>
Example 3:
Example 4:
DOM Model
● The Document Object Method (DOM) is a programming interface
for web documents. It represents the page so that programs can
change the document structure, style, and content.
● When a web page is loaded in the browser, another
representation of that document is created, called as DOM of that
page.
● The DOM represents HTML elements as nodes or objects; that way,
programming languages can interact with the page.
DOM Model
DOM Model
Why JS need DOM Model ?
● Javascript easily interpret this hierarchical DOM format.
● It can not understand HTML format but it easily understand DOM
format of the HTML document where each HTML element is
represented as object or node.
● Thus JS uses nodes to understand document and divide it into
three categories: Element node, Attribute node and Text node.
DOM
With the DOM model, JavaScript gets all the power it needs to create
dynamic HTML:
● JavaScript can change all the HTML elements and attributes in the
page.
● It can remove existing HTML elements and attributes.
● It can add new HTML elements and attributes.
● It can create new HTML events in the page.
● It can change all the CSS styles in the page
DOM Programming Interface
● The HTML DOM can be accessed with JavaScript (and with
other programming languages).
● In the DOM, all HTML elements are defined as objects.
● The programming interface is the properties and methods of
each object.
○ A property is a value that you can get or set (like changing the
content of an HTML element).
○ A method is an action you can do (like add or deleting an HTML
element).
How to Access the DOM
● Accessing elements in the DOM means finding specific parts of a
website and changing or manipulating them.
● JavaScript provides different methods to access the elements in the
DOM, such as getElementById, getElementsByClassName,
querySelector, and querySelectorAll. These methods allow you to
find an element based on its id, tagname, or classname and select it
for manipulation.
● For example, you can access a button on a webpage and change its
text or color when a user clicks on it. Or, you can access an image on
a webpage and change it to a different image when a user hovers
over it.
getElementById() method
The getElementById() method returns the elements of specified ID which
is passed to the function.
Every HTML element can be assigned a unique ID. If two or more elements
have the same ID, then the getElementByID() method returns the first
element.
Syntax:
document.getElementByID(id);
Syntax:
Object.innerHTML
(It is used to set the innerHTML property)
Object.innerHTML = value
(value: It represents the text content of the HTML element)
innerHTML property
innerHTML property
<!DOCTYPE html>
<html>
<body>
<h2>The innerHTML Property</h2>
<div id="myDIV">This is a div element.
<p id="myP">This is a p element.</p>
</div>
<script>
var x= document.getElementById("myDIV").innerHTML;
console.log(x);
</script>
</body>
</html>
innerHTML property
innerHTML property
<!DOCTYPE html>
<html>
<body>
<h2>The innerHTML Property</h2>
<p id="myP">This is a p element.</p>
<div id="myDIV">This is a div element.</div>
<script>
let text = "Hello World !!";
document.getElementById("myP").innerHTML=text;
document.getElementById("myDIV").innerHTML = text;
</script>
</body>
</html>
innerHTML property
<!DOCTYPE html>
<html>
<body>
<h2>The innerHTML Property</h2>
<p id="myP">This is a p element.</p>
<div id="myDIV">This is a div element.</div>
<script>
var text = "Hello World !!";
document.getElementById("myP").innerHTML;
var x= document.getElementById("myDIV").innerHTML=text;
console.log(x);
</script>
</body>
</html>
Difference Between innerHTML, innerText and textContent
The innerHTML property returns:
The text content of the element, including all spacing and inner
HTML tags.
The innerText property returns:
Just the text content of the element and all its children, without CSS
hidden text spacing and tags, except <script> and <style> elements.
The textContent property returns:
The text content of the element and all descendants, with spacing
and CSS hidden text, but without tags.
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_node_textcontent_innerhtml_innertext
getElementsByClassName() method
● The getElementsByClassName() method in Javascript returns an object
containing all the elements with the specified class names in the
document as objects.
● Each element in the returned object can be accessed by its index. The
index value will start with 0.
● This method can be called upon by any individual element to search for its
descendant elements with the specified class names.
● Syntax:
document.getElementsByClassName(classname);
getElementsByClassName() method
<!DOCTYPE html>
<html>
<body>
<h3>The getElementsByClassName()
Method</h3>
<div class="example">Element1</div>
<div class="example">Element2</div>
<script>
var x = document.getElementsByClassName("example");
</script>
</body>
</html>
getElementsByClassName() method
<!DOCTYPE html>
<html>
<body>
<h3>The getElementsByClassName()
Method</h3>
<div class="example">Element1</div>
<div class="example">Element2</div>
<script>
var x = document.getElementsByClassName("example");
x[0].innerHTML = "Hello World!";
x[1].innerHTML = "Welcome!";
</script>
</body>
</html>
getElementsByClassName() method
<!DOCTYPE html>
<html>
<body>
<h3>The getElementsByClassName() Method</h3>
<div class="example">Element1</div>
<div class="example">Element2</div>
<script>
var x = document.getElementsByClassName("example");
x[0].style.backgroundColor = "red";
x[1].style.backgroundColor = "green";
</script>
</body>
</html>
getElementsByClassName() method
<body> <script>
<p>Change the background color of all var x =
elements with class="example":</p> document.getElementsByClassName("
<div class="example"> example");
A div with class="example" for (let i = 0; i < x.length; i++) {
</div> x[i].style.backgroundColor = "green";
<br> }
<div class="example"> </script>
A div with class="example" </body>
</div>
<p>A <span
class="example">span</span> element
with class="example".</p>
Variable and data types in JS
● Variables are Containers for Storing Data.
● Variables name always starts with alphabet or underscore and not
with numbers.
● Note: JS is case sensitive. For example, Demo , deMo and demo
are 3 different variable.
● JavaScript Variables can be declared in 4 ways:
○ Automatically
○ Using var
○ Using let
○ Using const
Variable and data types in JS
1. Automatic declaration of variable
<script>
In this first example, x, y, and z
x = 5; are undeclared variables. They
y = 6; are automatically declared
when first used
z = x + y;
document.write("The value of z is: " + z);
</script>
2. Using var
<script>
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
4. Using const
You can change the elements of a constant array But you can NOT
reassign the array.
For example:
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERROR
4. Using const
Block scope: Declaring a variable with const is similar to let when it
comes to Block Scope.
For example:
const x = 10; const x = 2; // Allowed
// Here x is 10 {
{ const x = 3; // Allowed
}
const x = 2;
// Here x is 2 {
} const x = 4; // Allowed
// Here x is 10 }
Built in objects in JS
Built-in objects are used for simple data processing in the
JavaScript. It is a collection of properties and methods.
Built in objects in JS - Math object
Math object is a built-in static object. It is used for performing
complex math operations.
● Date object allows you to get and set the year, month, day, hour,
message.html
Event Handling in JS
Event Handling in JavaScript is a software routine that processes
actions that happen when a user interacts with the page, such as
mouse movements and keystrokes.
Event handlers can be assigned directly using the equal (=) operator
because they are attributes of HTML/DOM elements as well.
Syntax:
name_of_EventHandler = "The javaScript code / function which is
required to be executed"
Event Handling in JS
The names of event handlers typically start with on; for example,
onclick is the name of the event handler for the click event.
Event Handling in JS
Common HTML Event in JS
Common HTML Event in JS
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmouseover
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onmousemove
_leave_out
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_onchange
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onload
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown
Exception handling in JS
exception handling is a process or method used for handling the
abnormal statements in the code and executing them. It also enables
to handle the flow control of the code/program.
The try statement defines a code block to run (to try).