JavaScript DOM
JavaScript DOM
Method Description
document.getElementById() Finding an element by element id
document.getElementsByTagName() Finding elements by tag name
document.getElementsByClassName() Finding elements by class name
document.forms[] Finding elements by HTML element
objects
Finding HTML Elements
• Often, with JavaScript, you want to manipulate
HTML elements.
• To do so, you have to find the elements first.
There are a couple of ways to do this:
• Finding HTML elements by id
• Finding HTML elements by tag name
• Finding HTML elements by class name
• Finding HTML elements by HTML object
collections
Changing HTML Style
• Style
document.getElementById(id).style.property = new style
<body>
<script>
document.getElementById("p2").style.color = "blue";
</script>
</body>
After clicking
After clicking on the text an
alert box appears.
Finding HTML Elements by Class Name
• If you want to find all HTML elements with the same class
name. Use this method:
• document.getElementsByClassName(“abc");
• The example above returns a list of all elements with
class=“abc".
[Finding elements by class name does not work in Internet Explorer 5,6,7, and 8]
After clicking
document.getElementsByTagName()
View It
After clicking