Javascript Module 4 Notes
Javascript Module 4 Notes
With the object model, JavaScript gets all the power it needs
to create dynamic HTML:
• anchor
• Link
• Area
• Image
• Applet
• Layer
• anchor
The anchors collection returns a collection of all <a>
elements in the document that have a name attribute.
Ex:
Output:
Syntax
document.anchors
Properties
Property Description
Methods
Method Description
[index] Returns the <a> element from the collection with the specified index
(starts at 0).
Note: Returns null if the index number is out of range
item(index) Returns the <a> element from the collection with the specified index
(starts at 0).
namedItem(id) Returns the <a> element from the collection with the specified id.
Example
Get the HTML content of the first <a> element in the document:
var x = document.anchors[0].innerHTML;
HTML Tutorial
<html>
<body>
<p>Click the button to display the HTML content of the first anchor in the
document.</p>
<script>
function myFunction() {
var x = document.anchors[0].innerHTML;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
• Link
Ex:
Output:
Syntax
document.links
Properties
Property Description
Length Returns the number of <a> and/or <area> elements in the collection.
Methods
Method Description
[index] Returns the <a> and/or <area> element from the collection with the
specified index (starts at 0).
item(index) Returns the <a> and/or <area> element from the collection with the
specified index (starts at 0).
namedItem(id) Returns the <a> and/or <area> element from the collection with the
specified id.
More Examples
<html>
<body>
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p>
<a href="/html/default.asp">HTML</a><br>
<a href="/css/default.asp">CSS</a>
</p>
<p>Click the button to display the URL of the first link (index 0) in the document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.links[0].href;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
var x = document.links.item(0).href;
sun.htm
<html>
<body>
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm"
alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p>
<a href="/html/default.asp">HTML</a><br>
<a href="/css/default.asp">CSS</a>
</p>
<p>Click the button to display the URL of the first link (index 0) in the
document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.links.item(0).href;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Example
namedItem(id)
var x = document.links.namedItem("myLink").href;
default.asp
<html>
<body>
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm"
alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p>
<a id="myLink" href="/html/default.asp">HTML</a><br>
<a href="/css/default.asp">CSS</a>
</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.links.namedItem("myLink").href;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Example
Add a red border to the first link in the document:
<p>
<a href="/html/default.asp">HTML</a><br>
<a href="/css/default.asp">CSS</a>
</p>
<p>Click the button to add a red border to the first link in the
document.</p>
<p id="demo"></p>
<script>
function myFunction() {
document.links[0].style.border = "5px solid red";
}
</script>
</body>
</html>
Example
Loop through all links in the document, and output the URL (href) of each link:
var x = document.links;
var txt = "";
var i;
for (i = 0; i<x.length; i++) {
txt = txt + x[i].href + "<br>";
}
The result of txt will be:https
sun.htm
mercur.htm
venus.htm
default.asp
default.asp
<html>
<body>
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm"
alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
<p>
<a href="/html/default.asp">HTML</a><br>
<a href="/css/default.asp">CSS</a>
</p>
<p>Click the button to display the URL of each link in the document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.links;
var txt = "";
var i;
for (i = 0; i<x.length; i++) {
txt = txt + x[i].href + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
• Image
The images collection returns a collection of all
<img> elements in the document.
Ex:
Ouput:
Syntax
document.images
Properties
Property Description
Length Returns the number of <img> elements in the collection.
Methods
Method Description
[index] Returns the <img> element from the collection with the specified
index (starts at 0).
item(index) Returns the <img> element from the collection with the specified
index (starts at 0).
namedItem(id) Returns the <img> element from the collection with the specified id.
Example
[index]
Get the URL of the first <img> element (index 0) in the document:
var x = document.images[0].src;
<html>
<body>
<p>Click the button to display the URL of the first image (index 0) in the
document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.images[0].src;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Example
item(index)
Get the URL of the first <img> element (index 0) in the document:
var x = document.images.item(0).src;
klematis.jpg
<html>
<body>
<p>Click the button to display the URL of the first image (index 0) in the
document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.images.item(0).src;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Example
namedItem(id)
Get the URL of the <img> element with id="myImg" in the document:
var x = document.images.namedItem("myImg").src;
smiley.gif
<html>
<body>
<p>Click the button to display the URL of the img element with
id="myImg".</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.images.namedItem("myImg").src;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
Example
Add a black dotted border to the first <img> element in the document:
<p id="demo"></p>
<script>
function myFunction() {
var x = document.images[0];
x.style.border = "10px dotted black";
}
</script>
</body>
</html>
Example
Loop through all <img> elements in the document, and output the URL (src) of each image:
var x = document.images;
var txt = "";
var i;
for (i = 0; i<x.length; i++) {
txt = txt + x[i].src + "<br>";
}
klematis.jpg
klematis2.jpg
smiley.gif
<html>
<body>
<p>Click the button to display the URL of each img element in the
document.</p>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.images;
var txt = "";
var i;
for (i = 0; i<x.length; i++) {
txt = txt + x[i].src + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
• area
The Area object represents an
Output:
After clicking on button
6.applet
Applets are Internet programs, designed to communicate
across the Net and execute within a web browser.
Layer
Layers are objects on the map that consist of one or more
separate items, but are manipulated as a single unit. Layers
generally reflect collections of objects that you add on top of
the map to designate a common association.
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(date string)
new Date()
new Date() creates a new date object with the current date and time:
Example
const d = new Date();
new Date(dateString)
new Date(dateString) creates a new date object from a date string:
Example
const d = new Date("October 13, 2014 11:13:00");
These methods can be used for getting information from a date object:
Method Description
JavaScript Strings
A JavaScript string stores a series of characters like "John Doe".
String indexes are zero-based: The first character is in position 0, the second
in 1, and so on.
String Properties
Property Description
String Methods
All string methods return a new value. They do not change the original
variable.
Method Description
concat() Joins two or more strings, and returns a new joined strings
indexOf() Returns the position of the first found occurrence of a specified value in a
string
lastIndexOf() Returns the position of the last found occurrence of a specified value in a
string
string.match(regExp)
<script>
document.write(str.match(/eek/g));
</script>
Output:
eek, eek
In the above example, substring “eek” will match with the given string, and
when a match is found, it will return an array of string objects. Here “g” flag
indicates that the regular expression should be tested against all possible
matches in a string.
document.write(string.match(/eek/i);
repeat() Returns a new string with a specified number of copies of an existing string
<script>
document.write(str.repeat(2));
</script>
replace() Searches a string for a specified value, or a regular expression, and returns
a new string where the specified values are replaced
<script>
var str = "Visit Microsoft!";
document.write(str.replace("Microsoft", "Infosys"));
</script>
search() Searches a string for a specified value, or regular expression, and returns
the position of the match
document.write(myArr[3]);//doing
substr() Extracts the characters from a string, beginning at a specified start position,
and through the specified number of character
substring() Extracts the characters from a string, between two specified indices
Methods Description
Example 1
Let's see a simple example of isInteger() method.
<script>
var x=0;
var y=1;
var z=-1;
document.write(Number.isInteger(x));
document.write(Number.isInteger(y));
document.write(Number.isInteger(z));
</script>
Output:
Example 2
isInteger() method with some test cases.
1. <script>
2. function check(x,y)
3. {
4. return x/y;
5. }
6. document.write(Number.isInteger(check(12,2)));
7. document.write(Number.isInteger(check(12,5)));
8. </script>
Output:
true false
Output:
50
50.25
NaN
50
50.25
parseInt() It converts the given string into an integer number.
1. <script>
2. var a="50";
3. var b="50.25"
4. var c="String";
5. var d="50String";
6. var e="50.25String"
7. document.writeln(Number.parseInt(a)+"<br>");
8. document.writeln(Number.parseInt(b)+"<br>");
9. document.writeln(Number.parseInt(c)+"<br>");
10. document.writeln(Number.parseInt(d)+"<br>");
11. document.writeln(Number.parseInt(e));
12. </script>
Output:
50
50
NaN
50
50
Output:
50
-50
50.25
JavaScript Boolean
JavaScript Boolean is an object that represents value in two states: true or false. You
can create the JavaScript Boolean object by Boolean() constructor as given below.
let x = false;
But booleans can also be defined as objects with the keyword new:
Example
let x = false;
let y = new Boolean(false);
JavaScript Math
Method Description
Math.ceil(4.9); // returns 5
Math.ceil(4.7); // returns 5
Math.ceil(4.4); // returns 5
Math.ceil(4.2); // returns 5
Math.ceil(-4.2); // returns -4
Math.floor(4.9); // returns 4
Math.floor(4.7); // returns 4
Math.floor(4.4); // returns 4
Math.floor(4.2); // returns 4
Math.floor(-4.2); // returns -5
Math.round(4.9); // returns 5
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.round(4.2); // returns 4
Math.round(-4.2); // returns -4
Math.sign(-4); // returns -1
Math.sign(0); // returns 0
Math.sign(4); // returns 1
Math.sqrt(64); // returns 8
Math.trunc(4.9); // returns 4
Math.trunc(4.7); // returns 4
What is an Array?
In JavaScript, array is a single variable that is used to store different elements. It is
often used when we want to store list of elements and access them by a single
variable.
Declaration of an Array
There are basically two ways to declare an array.
Example:
Initialization of an Array
Example (for Method 1):
house1[0] = "1BHK"
house1[1] = "2BHK"
house1[2] = "3BHK"
house1[3] = "4BHK
varhome = newArray("!BHK");
unshift() – add one or more elements to the beginning of an array.
let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon");
pop() – remove an element from the end of an array.
Let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
shift() – remove the first element from an array.
let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
splice() – manipulate elements in an array such as deleting, inserting, and
replacing elements.