JavaScript Events and User Interaction: A Beginner's Guide

![[JS 강좌] 2강 - 개발 환경 구축하기 (VS Code, Chrome 개발자 도구)](/uploads/editor/JS-%EA%B0%95%EC%A2%8C-2%EA%B0%95-%EA%B0%9C%EB%B0%9C-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95%ED%95%98%EA%B8%B0-VS-Code-Chrome-%EA%B0%9C%EB%B0%9C%EC%9E%90-%EB%8F%84%EA%B5%AC-20260706-0dfe9b.webp)
What Are Events?
A website typically remains unchanged if users don't interact with it.
However, web pages respond to actions such as:
Button clicks
Keyboard input
Mouse movement
Scrolling
Checkbox selection
Image clicks
Page loading
These user actions or browser-triggered behaviors are called events.
JavaScript can detect these events and execute desired functionality.
Why Events Are Necessary
For example, let's create a login button.
No action should occur before the user clicks the button.
The login process should only trigger when the button is clicked.
This is where events come into play.
In other words,
executing code when specific conditions are met
this is event handling.
Using onclick
The simplest method is using onclick.
HTML
<button onclick="hello()">
Click Me
</button>
JavaScript
function hello() {
alert("Button clicked.");
}
Clicking the button displays an alert.
Connecting onclick in JavaScript
It's better to separate HTML and JavaScript.
HTML
<button id="btn">
Click
</button>
JavaScript
let btn = document.querySelector("#btn");
btn.onclick = function(){
alert("Hello.");
};
This method is also widely used.
addEventListener()
In professional development, addEventListener() is most commonly used.
HTML
<button id="btn">
Click
</button>
JavaScript
let btn = document.querySelector("#btn");
btn.addEventListener("click", function(){
alert("Clicked.");
});
The function executes when the click event occurs.
This approach is standard in professional work.
Using Arrow Functions
Arrow functions are frequently used now.
btn.addEventListener("click", () => {
alert("JavaScript");
});
The code becomes more concise.
Changing Content After Click
HTML
<h2 id="title">
JavaScript
</h2>
<button id="btn">
Change
</button>
JavaScript
let btn = document.querySelector("#btn");
btn.addEventListener("click", function(){
document.querySelector("#title").textContent =
"JavaScript Web Programming";
});
Clicking the button changes the title.
Getting Input Values
HTML
<input type="text" id="name">
<button id="btn">
Confirm
</button>
JavaScript
let btn = document.querySelector("#btn");
btn.addEventListener("click", function(){
let name =
document.querySelector("#name").value;
alert(name);
});
You can retrieve user input values.
input Event
You can check values as users type.
let input = document.querySelector("#name");
input.addEventListener("input", function(){
console.log(input.value);
});
This executes after each character is entered.
Commonly used in search autocomplete features.
change Event
This event triggers after input completion.
input.addEventListener("change", function(){
console.log(input.value);
});
It executes when the user finishes typing and clicks elsewhere.
keydown Event
This triggers when a keyboard key is pressed.
document.addEventListener("keydown", function(){
console.log("Key pressed.");
});
Checking for Enter Key
This is commonly used in login screens.
input.addEventListener("keydown", function(e){
if(e.key === "Enter"){
alert("Search started");
}
});
Pressing Enter can trigger a search.
mouseover Event
This executes when the mouse hovers over an element.
box.addEventListener("mouseover", function(){
console.log("Mouse entered");
});
mouseout Event
This executes when the mouse leaves an element.
box.addEventListener("mouseout", function(){
console.log("Mouse left");
});
dblclick Event
This is for double-click events.
btn.addEventListener("dblclick", function(){
alert("Double click");
});
When the Page Is Fully Loaded
This event executes after the webpage is ready.
window.addEventListener("load", function(){
console.log("Page loaded");
});
You can run code after the page has fully loaded.
Event Objects
When an event occurs, additional information is provided.
btn.addEventListener("click", function(e){
console.log(e);
});
The event object contains details such as:
Click position
Pressed key
Target element
and more.
preventDefault()
You can block default behavior.
For example, to prevent link navigation:
link.addEventListener("click", function(e){
e.preventDefault();
});
This is commonly used in professional development.
How Events Are Used in Practice
Events are one of the most frequently used features in web development.
Examples include:
Login buttons
Sign-up forms
Comment submission
Like buttons
Menu toggling
Dark mode switching
Image sliders
Search functionality
File uploads
Almost all web features rely on events.
This is why learning JavaScript is essential for web development.
Key Takeaways from This Lesson
Today we learned the basic concepts and practical applications of events in JavaScript.
Remember these key points:
Events represent user actions.
clickis the most commonly used event.addEventListener()is standard in professional work.Input values are retrieved using
value.inputandchangeare input events.You can detect keyboard presses with
keydown.preventDefault()blocks default behavior.Most web features rely on events for implementation.
Conclusion
Understanding events is the key to making websites respond to user actions.
All functionality involving button clicks, input validation, and mouse movements relies on event handling.
Try running the examples in this lesson by clicking buttons and changing inputs. Directly experiencing these small changes is one of the fastest ways to learn JavaScript.
In our next lesson, we'll cover form validation and user input checking used in sign-up and login screens, learning how to process valid data correctly.
FAQ
What is an event in JavaScript?
An event is a user action or browser behavior that triggers code execution, such as button clicks, keyboard input, or page loading.
Why use addEventListener() instead of onclick?
addEventListener() allows multiple handlers for the same event and is the standard method used in professional development.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [JS 강좌] 8강 - 이벤트(Event)와 버튼 클릭, 입력 처리하기