KOSUNI KOREA
Korean News in English
Technology

JavaScript DOM Basics: Selecting and Modifying HTML Elements

Published October 17, 2026 · 1 views
JavaScript DOM Basics: Selecting and Modifying HTML Elements

[JS 강좌]  2강 - 개발 환경 구축하기 (VS Code, Chrome 개발자 도구)


What is DOM (Document Object Model)?

JavaScript can control web pages because of the DOM (Document Object Model).

When a web browser reads an HTML file, it doesn't just create a visual display—it transforms the HTML into an object (Object) structure.

This structure is called the DOM.

In simple terms,

a representation of HTML in a format JavaScript can understand

JavaScript uses the DOM to find, modify, add, or delete HTML elements.


Why Do We Need DOM?

You cannot change page content with HTML alone.

For example:

  • Changing a title

  • Hiding a button

  • Replacing an image

  • Opening a menu

  • Adding comments

  • Increasing likes

All these features are implemented by JavaScript manipulating the DOM.

In other words,

The DOM acts as a bridge connecting JavaScript and HTML.


Selecting HTML Elements

The first step is selecting the desired HTML element.

Let's prepare an example:

<h1 id="title">JavaScript Course</h1>

Now let's select this element in JavaScript.


getElementById()

This is the most basic selection method.

let title = document.getElementById("title");

console.log(title);

Selects the element with id="title".

This method is commonly used in real-world development.


querySelector()

Today, querySelector() is most frequently used.

let title = document.querySelector("#title");

The result is the same as getElementById().

However, it allows using CSS selectors directly, making it more convenient.


Selecting by Class

HTML

<p class="text">First sentence</p>

JavaScript

let text = document.querySelector(".text");

The . represents a class selector.


Selecting by Tag

let title = document.querySelector("h1");

Selects the first <h1> tag.


Selecting Multiple Elements

If multiple elements share the same class, use:

let items = document.querySelectorAll(".menu");

For example:

<li class="menu">Home</li>
<li class="menu">About</li>
<li class="menu">Contact</li>

JavaScript

let menus = document.querySelectorAll(".menu");

menus.forEach(item => {

    console.log(item);

});

This retrieves all elements at once.


Changing HTML Content

HTML

<h1 id="title">JavaScript</h1>

JavaScript

let title = document.querySelector("#title");

title.innerHTML = "JavaScript Web Programming";

The title will change when executed.


textContent

If you only want to change the text, use textContent.

title.textContent = "New Title";

In practice, developers often use textContent for security reasons.


innerHTML vs textContent

innerHTML

title.innerHTML = "<b>JavaScript</b>";

Displays text in bold.

textContent

title.textContent = "<b>JavaScript</b>";

Displays HTML tags as plain text.


Changing Styles

JavaScript can also modify CSS styles.

title.style.color = "blue";

The text color changes to blue.

You can also change background colors.

title.style.backgroundColor = "yellow";

Font size

title.style.fontSize = "40px";


Changing Attributes

You can also change image addresses.

HTML

<img id="photo" src="cat.jpg">

JavaScript

let photo = document.querySelector("#photo");

photo.src = "dog.jpg";

The image will change.


Adding and Removing Classes

You can add classes:

title.classList.add("active");

Remove them:

title.classList.remove("active");

Toggle:

title.classList.toggle("active");

This is commonly used for features like dark mode or opening menus.


Adding New HTML Elements

You can add new elements to HTML.

let list = document.querySelector("#list");

list.innerHTML += "<li>New Menu</li>";

A new item is added to the list.


Deleting Elements

HTML

<p id="msg">Hello.</p>

JavaScript

document.querySelector("#msg").remove();

The element is removed from the screen.


Getting Input Values

This is commonly used in forms.

HTML

<input type="text" id="name">

JavaScript

let value = document.querySelector("#name").value;

console.log(value);

You can retrieve user input values.


Changing Content After Button Click

Let's look at an example:

HTML

<h2 id="title">Welcome.</h2>

<button id="btn">Change</button>

JavaScript

let btn = document.querySelector("#btn");

btn.onclick = function(){

    document.querySelector("#title").textContent = "Starting JavaScript!";

};

Clicking the button changes the title.

Events will be covered in more detail in the next lesson.


How Is DOM Used in Real Development?

The DOM is used on almost all websites.

For example:

  • Login screens

  • Comment submission

  • Board listings

  • Product lists

  • Shopping carts

  • Menu opening

  • Dark mode

  • Modal windows

  • Search result display

Most features are implemented by manipulating the DOM.

The main reason to learn JavaScript is ultimately to be able to freely manipulate the DOM.


Key Takeaways from This Lesson

Today we learned about the DOM, the core technology that allows JavaScript to control HTML.

Remember these key points:

  • The DOM is an object-based representation of HTML.

  • querySelector() is the most commonly used method for selecting elements.

  • querySelectorAll() selects multiple elements.

  • innerHTML changes both text and HTML structure.

  • textContent only changes the text content.

  • You can modify CSS using style.

  • Use classList to add or remove classes.

  • Retrieve input values using value.


Conclusion

This lesson marks a crucial turning point in learning JavaScript.

So far, we've focused on syntax; now we begin to interact directly with web pages.

Once you master the DOM, you'll be able to implement features like button clicks, menu navigation, comment posting, and board functionality.

In the next lesson, we'll learn about events (Event), which detect user actions like button clicks, keyboard input, and mouse movements, and how to handle them effectively.

FAQ

What is the DOM?

DOM (Document Object Model) is a programming interface that represents an HTML document as objects, allowing JavaScript to manipulate it.

Why use querySelector over getElementById?

querySelector allows using CSS selectors directly and is more flexible for selecting elements in modern web development.

What's the difference between innerHTML and textContent?

innerHTML changes both HTML content and structure, while textContent only changes plain text without interpreting HTML tags.

Korean Source

This article is an English localization of a Korean-language report from BlogScore. Original: [JS 강좌] 7강 - DOM이란? HTML 요소 선택하고 수정하기