Unit II - Java Script, DOM JQuery
Unit II - Java Script, DOM JQuery
Unit II - Java Script, DOM JQuery
Unit- II
Topic:
Java Script, DOM, JQuery, AngularJS
JavaScript:
● Introduction to JavaScript,
● JavaScript in perspective,
● basic syntax,
● variables and data types,
● statements,
● operators,
● literals,
● functions,
● objects,
● arrays,
● built in objects,
● JavaScript debuggers
JS- Java Script
Java Script
• Sections of a page
• Content loading and
• React to user actions, appearing and
changing dynamically
disappearing
What Can JavaScript Do?
Script in Script in
<head>...</head> section <body>...</body> section
Example:
<script >
JavaScript code
</script>
Java Script Example- 1
Print Hello Message
<html>
<body>
<script>
Script Code in HTML document.write("Hello World")
file in Body Tag
</script>
</body>
</html>
Java Script Example- 2
Print Hello Message
<html>
<head>
<script>
Script Code in HTML document.write("Hello World")
file in Head Tag
</script>
</head>
</html>
Java Script Example- 3
Print Hello Message
<html>
<head>
<script> Print Message using
alert
alert("Hello World")
</script>
</head>
</html>
Java Script Example- 4
Print Hello Message
<html>
<head>
<script>
function sayHello()
{ alert("Hello World") } Print Message after
clicking button
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>
Javascript Variables
JS- Java Script Variables
4. Using Nothing
JS- Java Script Variables
Example
Example ByBy using
using letVar Example By using Const
letvar
x =x5;
= 5; const x = 5;
letvar
y =y6;
= 6; const y = 6;
letvar
z =zx=+xy;+ y; let z = x + y;
● 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.
Javascript Data Types
Java Script Data Types
Primitive Non-Primitive
Java Script Data Types
Primitive
2. Code Blocks:
· JavaScript statements can be grouped together inside curly brackets. { }
· Such groups are known as code blocks. The purpose of grouping is to define statements to be executed together.
3. White Space:
· Javascript ignores multiple white spaces.
4. Line Length and Line Breaks:
· Javascript code preferred line length by most programmers is upto 80 characters.
· The best place to break a code line in Javascript, if it doesn’t fits, is after an operator.
JavaScript Statements
5. Keywords:
· Keywords are reserved words and cannot be used as variable name.
· A Javascript keyword tells about what kind of operation it will perform.
· Some commonly used keywords are:
1. break: This is used to terminate a loop or switch.
2. continue: This is used to skip a particular iteration in a loop and move to next iteration.
3. do…. while: In this the statements written within do block are executed till the condition in while is true.
4. for: It helps in executing a block of statements till the condition is true.
5. function: This keyword is used to declare a function.
6. return: This keyword is used to exit a function.
7. Var, Let, Const: This keyword is used to declare a variable.
8. switch: This helps in executing a block of codes depending on different
Javascript Operators
JavaScript Operators:Arithmetic Operators
JavaScript Operators:Assignment Operators
JavaScript Operators:Comparison Operators
JavaScript Operators:Logical Operators
Javascript Literals
JavaScript- Literals
A string literal is a combination of zero or more characters enclosed within a single(') or double
quotation marks (").
● An array literal is a list of zero or more expressions representing array elements that
are enclosed in a square bracket([]).
● Whenever you create an array using an array literal, it is initialized with the elements
specified in the square bracket.
❏ Anonymous Function
❏ Named Function
<script>
var showMessage = function (){
alert("Hello World!");
};
showMessage();
</script>
Named Function without argument
function SayHello() {
alert("Hello ");
}
SayHello();
Named Function with argument
function SayHello(firstName) {
alert("Hello " + firstName);
}
SayHello("Bhavana");
Function with return statement
● 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”
Java Script -Arrays
How to access Values of Arrays in Javascript
● Length
● Reverse
● Sort
● Push
● Pop
● forEach
Javascript Objects
Java Script -Objects
● Objects are variables too. But objects can contain many values.
● The values are written as name : value pairs
● Method -1
○ var person = {name:"Bhavana", age:40};
● Method -2
○ var person = new Object();
○ person.name = "Bhavana";
○ person.age = 40;
Java Script -Objects
How to Read/Access Properties of an Object in Javascript
SQRT2 - Square root of 2 random() - Returns a pseudo random number between 0 and 1
sqrt(a) - Returns square root of a
Javascript Debugger
Javascript Debugger
● Built-in debuggers can be turned on and off, forcing errors to be reported to the user.
● With a debugger, you can also set breakpoints (places where code execution can be stopped), and
examine variables while the code is executing.
● You activate debugging in your browser with the F12 key, and select "Console" in the debugger
menu.
● The debugger keyword stops the execution of JavaScript, and calls (if available) the debugging
function.
● With the debugger turned on, this code will stop executing before it executes the third line.
❏ 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
DOM
DOCUMENT OBJECT MODELING
DOM-
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
DOM- Tree
<html>
<head>
<title> My Text </title>
</head>
<body>
<h1> My Header </h1>
<p> My Paragraph </p>
</body>
</html>
DOM Levels
DOM Levels
Methods Description
Methods Example
.innerHTML document.getElementById(“demo”).innerHTML
..Value document.getElementById(“t1”).Value
DOM- Examples: getElementById
<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
<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
<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- 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>
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>
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>
JQUERY
JQuery
jQuery is a JavaScript
jQuery is easy to learn.
Library.
JQuery
Google
Netflix
IBM
Adding jQuery to Your Web Pages
Way
Download the jQuery library from jQuery.com
1
Way
Include jQuery from a CDN, like Google
2
jQuery CDN
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
jQuery Syntax
$(selector).action()
<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>
Web Technology
Unit- II
Topic:
AngularJS-
AngularJS
• Syntax
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"> </script>
AngularJS
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web
applications.
• A Model View Controller pattern is made up of the following three parts −
1. Model − It is the lowest level of the pattern responsible for maintaining data.
2. View − It is responsible for displaying all or a portion of the data to the user.
3. Controller − It is a software Code that controls the interactions between the Model and View.
Steps to create AngularJS
1. ng-app − This directive starts an AngularJS Application. It is also used to load various AngularJS
modules in AngularJS Application.
<div ng-app = ""> ... </div>
2. ng-init − This directive initializes application data. It is used to put values to the variables to be
used in the application.
<div ng-app="" ng-init="firstName='John'">
AngularJS - Directives
3. ng-model − This directive binds the values of AngularJS application data to HTML input controls.
<p>Enter your Name: <input type = "text" ng-model = "name"></p>
4. ng-repeat − This directive repeats html elements for each item in a collection.
<ol>
<li ng-repeat = "country in countries"> {{ country.name}} </li>
</ol>
AngularJS Example
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"> </script>
<body>
<div ng-app="">
Name: <input type="text" ng-model="name">
<p ng-bind="name"> </p>
</div>
</body>
</html>
AngularJS Example Explained
Example explained:
• AngularJS starts automatically when the web page has loaded.
• The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.
• The ng-model directive binds the value of the input field to the application variable name.
• The ng-bind directive binds the innerHTML of the <p> element to the application variable name.
AngularJS Expression
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
<body>
OutPut
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS - Controllers
AngularJS application mainly relies on controllers to control the flow of data in the application.
• A controller is defined using ng-controller directive.
• Each controller accepts $scope as a parameter which refers to the application/module that
controller is to control.
• <div ng-app="myApp" ng-controller="myCtrl">
AngularJS Controllers- Example
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>