Is this your first lecture for this class? Please introduce yourself briefly.
- Job
- Education
- City
- Why you love programming (if you do)
The purpose of this class is to introduce to the student:
- How a webpage is made up of objects (DOM)
- How JavaScript can be used to manipulate those objects (DOM manipulation)
- Show a list of commonly used browser defined functions
- How to combine add and remove DOM elements
FIRST HALF (12.00 - 13.30)
The Document Object Model (DOM) is an object-oriented representation of a web page(HTML document) which the web browsers make available to JavaScript for manipulation. Inside a JavaScript file, we can access the DOM through a global object called document or window.document.
It is not a programming language but without it JavaScript would not have any knowledge of our web page/HTML document.
<!DOCTYPE html>
<html>
<head>
<title>My title</title>
</head>
<body>
<h1>My header</h1>
<a href="https://www.w3schools.com/js/pic_htmltree.gif">My link</a>
</body>
</html>-
Finding DOM elements in HTML page
document.getElementById(id)- Find an element by element iddocument.getElementsByTagName(name)- Find elements by tag namedocument.getElementsByClassName(name)- Find elements by class name
-
Adding and Deleting elements in HTML page
document.createElement(element)- Create a new HTML elementdocument.removeChild(element)- Remove an HTML elementdocument.appendChild(element)- Add an HTML element
-
Changing existing HTML elements
element.innerHTML- Change the content/layout of the elementelement.innerText- Change just the text of the elementelement.setAttribute(attribute, value)- Set/Change attribute of an element
- Note:
getElementsByTagNameandgetElementsByClassNamereturns a list of all matched elements. However, this is not the usual JavaScript array but an HTMLCollection List. A detailed list of APIs available on the DOM can be found here.
-
Create an HTML form element
-
Create an HTML input(type text) element and set its placeholder as "First Name"
-
Create another HTML input(type text) element and set its placeholder as "Last Name"
-
Add both these elements to the form element
-
Create a button element and add these properties to it:
a. Set its text to "Click Me"
b. Set its id to "button"
c. Set its type to "button"
-
Add button element to the form
-
Add the form element to main element
SECOND HALF (14.00 - 16.00)
