Unit 2
Unit 2
Unit-II
By
Prof. Priyanka S. Jadhav
(Assit. Prof., Comp. Dept. , SKNCOE, Pune)
Contents
Introduction to JavaScript
Basic Syntax
Variables and Data Types
Statements
Operators
Literals
Functions
Objects
Arrays
Built in Objects
JavaScript Debuggers
Contents
DOM: Introduction to Document Object Model
DOM history and levels
Intrinsic event handling
Modifying element style
The document tree
DOM event handling
jQuery
Introduction to JavaScript
• JavaScript is an object-based scripting
language which is lightweight and cross-platform.
• JavaScript is used to create interactive websites.
• Application of JavaScript :
Client-side validation,
Dynamic drop-down menus,
Displaying date and time,
Displaying pop-up windows and dialog boxes (like an alert
dialog box, confirm dialog box and prompt dialog box),
Displaying clocks etc.
Advantages of Client-Side
JavaScript
• Less server interaction − You can validate user input
before sending the page off to the server. This saves
server traffic, which means less load on your server.
• Immediate feedback to the visitors − They don't
have to wait for a page reload to see if they have
forgotten to enter something.
• Increased interactivity − You can create interfaces
that react when the user hovers over them with a
mouse or activates them via the keyboard.
• Richer interfaces − You can use JavaScript to include
such items as drag-and-drop components and sliders
to give a Rich Interface to your site visitors.
JavaScript Syntax
• JavaScript can be implemented using JavaScript
statements that are placed within the <script>...
</script> HTML tags in a web page.
• Example:
JavaScript Datatypes
• JavaScript Data types (Primitive)
1. Number
2. String
3. Boolean
4. Undefined
5. Null
• Example:
JavaScript Operators
Example:
JavaScript – Switch Case
switch..case statement :
1. for loop
2. while loop
3. do-while loop
4. for-in loop
JavaScript – for loop
The for loop iterates the elements for the fixed
number of times.
It should be used if number of iteration is known.
Syntax :
Example :
JavaScript – while loop
The while loop iterates the elements for the infinite
number of times.
It should be used if number of iteration is not known.
Syntax Example:
JavaScript – do while loop
The do while loop iterates the elements for the infinite
number of times like while loop.
But, code is executed at least once whether condition
is true or false.
Syntax Example:
JavaScript Functions
JavaScript functions are used to perform operations
Syntax:
JavaScript Functions
Example:
Function
Event
JavaScript – Function with Arguments
Example:
Argument/Parameter
Function
Passing Argument
JavaScript – Function with Return
Value
Example:
“Hello!!
Return ”;
JavaScript – Anonymous Function
Example:
<script>
var showMessage =
function (){
alert("Hello World!");
};
showMessage();
</script>
JavaScript Objects
Object is an entity having state and behavior
properties
method
Syntax:
Example:
Creating Objects in JavaScript
Way 1I: By creating instance of Object
Syntax:
Example:
Creating Objects in JavaScript
Way III: By using an Object Constructor (new
keyword)
Example:
Built-In Objects in JavaScript
JavaScript – Array Object
JavaScript array is an object that represents a
collection of elements.
Syntax:
Example:
Creating Array in JavaScript
Way 1I: By creating instance of Array
Syntax:
Example:
can mention Array size here
Creating Array in JavaScript
Way III: By using Array Constructor
Example:
Syntax:
Example :
JavaScript – String Object
Methods of String Object
<html>
<head>
<title> My Text
</title>
</head>
<body>
<h1> My Header
</h1>
<p> My Paragraph
</p>
</body>
</html>
DOM - Methods
Methods of document object :
DOM - Properties
Properties of document object :
DOM Examples -
getElementById
Example :
<html>
<head>
<script>
function f1() {
var a=
document.getElementById("t1").value;
alert(a); }
</script>
</head>
<body>
<input type="text" id="t1" >
<input type=“button” value="click"
onclick="f1()">
</body>
</html>
DOM Examples-
getElementsByTagName
Example :
<html>
<head>
<script>
function f1() {
document.getElementsByTagName("p")[0].innerHTML="HTML";
</script>
</head>
<body>
<p> Hello</p>
<p > How Are You </p>
<input type=“button” value="click" onclick="f1()">
</body>
</html>
DOM Examples-
getElementsByClassName
Example :
<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>
DOM – JavaScript Events
The change in the state of an object is known as
an Event.
For example,
Click Event - when a user clicks over the browser or
over any HTML element
1. HTML/DOM manipulation
2. CSS manipulation
3. HTML event methods
4. Effects and animations
5. AJAX
6. Utilities
jQuery – How to Add?
Two ways to Add jQuery in Your Web Pages :
jQuery – CDN
CDN link for adding jQuery :
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/j
query.min.js">
</script>
</head>
jQuery – Syntax
jQuery – How to Use
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>
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>
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>
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>
Any Questions?