Grade-9-Lecture-3rd-Quarter
Grade-9-Lecture-3rd-Quarter
Please take note that you can place any number of scripts in an HTML document. To improve page load,
it is recommended for you to place all your scripts at the bottom of the BODY section.
CHANGING HTML CONTENT USING JAVASCRIPT
To manipulate HTML element, it is best to learn about HTML DOM. What is HTML DOM? When a web
page is loaded, the browser produces a Document Object Model (also known as DOM) of the page. The
DOM defines the following:
HTML elements as objects
Properties of each HTML element
Methods to access all HTML element
Events associated to each HTML element
You can access the DOM using JavaScript.
Example
In this example, the code changes the content of the paragraph element with ID = “demo”. With the use
of getElementById method, we were able to access the <p> element. As a review, a method is an action
you can do to an element. To change the content of the element, we need to set
the innerHTML property to our desired value. A property is a value of an element that you can access or
change.
The following are example of methods and properties you can use to access and manipulate HTML
elements:
As a developer, it would be a hassle for you to type the HTML codes to write each item on the web page.
Picture this, you have to display 30 items for men’s clothes, 30 for women’s clothes, 30 for men’s
accessories, 30 for women’s accessories, 30 for men’s shoes and another 30 for women’s shoes. It
would take a huge amount of time to input the code for each item right? What if you want to update the
price of an item or want to add another item or remove an item from your list? The best thing about
JavaScript is that it can create a program logic to write HTML contents for you.
In writing the code for loading each product item on the webpage, you might have notice that there is a
recurring pattern. There are information that is changing specific for a particular product. In this lecture,
you’re going to learn about JavaScript Object and how we can use it in storing information about our
product items. You’ll also know about JavaScript looping statement to save us time and effort in writing
repetitive codes. And last is applying JavaScript arrays in storing and accessing specific product items.
In the given example, the object person is used as container for the data value. Objects are variables too
but not like other variables it can contain more than one value.
In the code above, more than one value are assigned to variable person. The values are written as name
and value separated by colon. These pairs are called properties.
Please take note that spaces and line breaks are not important. Defining an object can span to multiple
lines.
Aside from properties, JavaScript object has methods. For example, in real life, a pen can write, right? In
JavaScript, methods are actions that can be performed on objects. In our example below,
the person object can return a message.
In this example, there are three values stored in variable persons “William”, “Richard” and “Bruce”. You
can access the values by referring to their index numbers.
To create an array, you can use an array literal just like the example above. You can also use the
JavaScript keyword new...
Example:
Performance-wise, using the array literal method is recommendable in creating arrays, although both
methods do exactly the same.
To access the elements of an array, here is an example:
document.write("Name: "+persons[0]);
As what was mentioned earlier, you can access an array element by referring to their index. Array
indexes start with 0. So that means that persons[0] is the first element, persons[1] is the second, so on
and so forth.
Arrays can have different objects in one array. You can have objects, functions and arrays in an array.
Arrays are considered as a special type of objects. The difference between arrays and objects is that
arrays make use of index numbers to access its elements while objects makes use of names to access its
properties/members.
Example: