Web Unit-4
Web Unit-4
<p id="demo1"></p>
<script>
const person1 = {};
person1.firstName = "John";
person1.lastName = "Doe";
person1.age = 50;
person1.eyeColor = "blue";
document.getElementById("demo1").innerHTML =
person1.firstName + " is " + person.age + " years old.";
</script>
By using an object constructor
It is considered good practice to name constructor functions with an upper-case first
letter.
In a constructor function this does not have a value. It is substitute for the new object.
The value of this will become the new object, when a new object is created.
Example:
<p id="demo"></p>
<script>
// Constructor function for Person objects
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}
// Create a Person object
<p id="demo1"></p>
<script>
const d1 = new Date();
document.getElementById("demo1").innerHTML = d1.toUTCString();
</script>
<p id="demo2"></p>
<script>
const d2 = new Date();
document.getElementById("demo2").innerHTML = d2.toDateString();
</script>
<p id="demo3"></p>
<script>
const d3 = new Date();
document.getElementById("demo3").innerHTML = d3.toISOString();
</script>
Date(milliseconds)
JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC
(Universal Time Coordinated).
Example:
<p>100000000000 milliseconds from Jan 1, 1970, is approximately Mar 3, 1973:</p>
<p id="demo"></p>
<script>
const d = new Date(100000000000);
document.getElementById("demo").innerHTML = d;
</script>
<p id="demo1"></p>
<script>
<p id="demo2"></p>
<script>
const d2 = new Date();
document.getElementById("demo2").innerHTML = d2.getDate();
</script>
<p id="demo3"></p>
<script>
const d3 = new Date();
document.getElementById("demo3").innerHTML = d3.getHours();
</script>
<p id="demo4"></p>
<script>
const d4 = new Date();
document.getElementById("demo4").innerHTML = d4.getMinutes();
</script>
4.2.2 Date Methods:
Method Description
getFullYear() Get the year as a four digit number (yyyy)
getMonth() Get the month as a number (0-11)
getDate() Get the day as a number (1-31)
getHours() Get the hour (0-23)
getMinutes() Get the minute (0-59)
getSeconds() Get the second (0-59)
getMilliseconds() Get the millisecond (0-999)
getTime() Get the time (milliseconds since January 1, 1970)
getDay() Get the weekday as a number (0-6)
getDate()
Example:
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>
getDay()
Example:
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
getMonth()
Example:
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
getHours()
Example:
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>
setDate()
Example:
<p id="demo"></p>
<script>
const d = new Date();
d.setDate(15);
document.getElementById("demo").innerHTML = d;
</script>
setMonth()
Example:
<p id="demo"></p>
<script>
const d = new Date();
d.setMonth(4);
document.getElementById("demo").innerHTML = d;
</script>
toString()
Example:
<p id="demo"></p>
<script>
const d = new Date();
let text = d.toString();
document.getElementById("demo").innerHTML = text;
</script>
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
JavaScript can change all the HTML elements in the page
JavaScript can change all the HTML attributes in the page
JavaScript can change all the CSS styles in the page
JavaScript can remove existing HTML elements and attributes
JavaScript can add new HTML elements and attributes
JavaScript can react to all existing HTML events in the page
JavaScript can create new HTML events in the page
The getElementById() method is one of the most common methods in the HTML DOM.
It is used almost every time you want to read or edit an HTML element.
Example:
<h1 id="demo">The Document Object</h1>
<script>
const myElement = document.getElementById("demo");
myElement.style.color = "red";
</script>
<h1 id="demo1">DOM</h1>
<script>
document.getElementById("demo1").style.color = "red";
</script>
getElementByName()
The getElementsByName() method returns a collection of elements with a specified name.
Example:
<p>First Name: <textarea name="fname" type="text" value="Michael"></textarea></p>
<p>First Name: <input name="fname" type="text" value="Michael"></textarea></p>
<p>First Name: <textarea name="fname1" type="text" value="Michael"></textarea></p>
<p>The tag name of the first element with the name "fname" is:</p>
<p id="demo"></p>
<script>
let elements = document.getElementsByName("fname");
document.getElementById("demo").innerHTML = elements[1].tagName;
</script>