Lecture11-Javascript HTML DOM
Lecture11-Javascript HTML DOM
HTML DOM
The HTML DOM
Every element on an HTML page is accessible in JavaScript through the
DOM: Document Object Model
If you want to access any element in an HTML page, you always start with accessing the
document object.
Action Example
For example this javascript statement will return the first paragraph element of
class main:
<body>
<p>my first paragraph</p>
<p class="main">my first main paragraph</p>
<p class="main">my second main paragraph</p>
document.querySelector("p.main");
<a href="http://www.google.com">google</a>
</body>
Finding HTML Elements
If you want to find all HTML elements that match a specified CSS selector (id, class
names, types, attributes, values of attributes, etc), use the querySelectorAll()
method.
For example this javascript statement will return all paragraph elements of class
main:
<body>
<p>my first paragraph</p>
<p class="main">my first main paragraph</p>
<p class="main">my second main paragraph</p>
document.querySelectorAll("p.main");
<a href="http://www.google.com">google</a>
</body>
Finding HTML Elements
querySelectorAll() method will return a list of all HTML elements that match the
specified CSS query.
<body>
<p>my first paragraph</p>
<p class="main">my first main paragraph</p>
pars[0]
pars[1] <p class="main">my second main paragraph</p>
<a href="http://www.google.com">google</a>
</body>
Changing HTML Elements
The HTML DOM allows JavaScript to change the content of HTML elements.
The easiest way to modify the content of an HTML element is by using the
innerHTML property.
As a general rule of thumb, in order to get the style property name in javascript, you
should change the CSS property name to camelCase!
CSS property Javascript property
color color
background-color backgroundColor
margin-top marginTop
Adding HTML Elements
To add a new element to the HTML DOM, you must create the element (element
node) first, and then append it to an existing element.
parentElement.appendChild(childElement);
This is the element you
want to append the This is the child element
child element to you want to nest inside
the parent element
Adding HTML Elements
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
If you don't want that you can use the insertBefore() method:
parentElement.insertBefore(newElement, existingElement)
This is the parent This is the new element This is the existing
element you want to you want to insert inside element inside parent
insert the new element the parent element and element, for which you
inside it before the existing want to insert the new
element element before it
Adding HTML Elements
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
Then you can use this syntax to remove the element you want:
parentElement.removeChild(childElement)
parentElement.replaceChild(newElement, oldElement)
To achieve this we have to add an event listener that fires when a user causes any
event e.g. clicks a button
element.addEventListener(event, eventHandler)
This is the element you This is the event you This is the name of the
want to capture the want to capture e.g. function you want to call
events on 'click' or 'mouseover' when the event is fired
Javascript DOM Events
You can add many eventHandlers for the same or different events to the same
element:
element.addEventListener('click', function1);
element.addEventListener('click', function2);
element.addEventListener('keyup', function3);
Javascript DOM Events
You can also remove event handlers that have been attached with the
addEventListener() method:
element.removeEventListener(event, eventHandler)
This is the element you This is the event name This is the name of the
want to remove the you want to remove the function you have used
eventListener from eventListener for as the eventHandler for
the eventListener you
want to remove
Javascript DOM Events
element.addEventListener('click', function1);
element.addEventListener('click', function2);
element.addEventListener('keyup', function3);