Code has been added to clipboard!

Tips and Tricks on JavaScript DOM Methods With Real Examples

Reading time 2 min
Published Aug 8, 2017
Updated Oct 2, 2019

One of the most important things you should know about if you want to use JavaScript in web development is Document Object Model, or simply DOM. Understanding what is DOM, how it works, and how you can access it allows you to have more control over your code.

In this lesson, you will learn about JavaScript DOM methods, how to target elements in the HTML with JavaScript, and how to manipulate them.

JavaScript DOM Methods: Main Tips

  • A JavaScript DOM method exists as an action that can be executed on HTML elements.
  • HTML elements have properties that are basically changeable values.
  • In DOM HTML elements are classified as objects.
  • innerHTML property is the simplest way to display something inside an element.

Using Javascript DOM

In the DOM JavaScript example below, we target the <p> element using the getElementById method and grabbing the <p> element's id id="demoz". The <p> element's content is manipulated using the innerHTML property:

Example
document.getElementById("example").innerHTML = "Hi there!";

Note: you can use the innerHTML property to change all of the HTML elements' content. This works for <html> and <body> tags, too.

JavaScript DOM Methods: Summary

  • One of the simplest ways of targeting an HTML element often used in JavaScript is the getElementById method.
  • Displaying content in an element is most easily and commonly performed by using the innerHTML property. You can learn more about using it here.
  • To get a better understanding on what is DOM, check out our JavaScript HTML DOM navigation tutorial.
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen