GIRLSCRIPT EDUCATION OUTREACH PROGRAM
Day - 3
The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.
The HTML DOM model is constructed as a tree of Objects:
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</script>
</body>
</html>
getElementById is a method, while innerHTML is a property.
The most common way to access an HTML element is to use the id of the element.
In the example above the getElementById method used id="demo" to find the element.
The easiest way to get the content of an element is by using the innerHTML property.
The innerHTML property is useful for getting or replacing the content of HTML elements.
Methods for
Method | Description |
---|---|
document.getElementById(id) | Find an element by element id |
document.getElementsByTagName(name) | Find elements by tag name |
document.getElementsByClassName(name) | Find elements by class name |
document.querySelector(id / class) | Find by query |
document.querySelectorAll(id / class) |
Find all elements with the same query |
Properties for
Property | Description |
---|---|
element.innerHTML = New html content | Change the inner HTML of an element |
element.attribute = New value | Change the attribute value of an HTML element |
element.style.property = New style | Find elements by class name |
Methods for
Method | Description |
---|---|
click | Fires when the mouse is pressed and released on an element |
submit | Fires when a form is submitted |
focus | Fires when an element (such as an input) receives focus |
blur | Fires when an element loses focus |
keypress | Fires continuously while a key is pressed |