JavaScript Lesson 10 — Build a Study List with the DOM and addEventListener
So far, most results have appeared in the console. Let us finish the opening section with a usable page: enter a study topic, press Add, and see another list item. This combines the pieces we have learned.
Prepare the page elements
Use this body in index.html:
<h1>Çalışma listem</h1>
<form id="topic-form">
<label for="topic">Çalışılacak konu</label>
<input id="topic" name="topic" required>
<button type="submit">Ekle</button>
</form>
<p id="feedback" role="status"></p>
<ul id="topics"></ul>
<script src="app.js" defer></script>
The form, input, feedback paragraph and list have separate identifiers. The label targets its input. The submit button uses the form's submission flow, which also supports Enter from the field. The status role supports announcing changed feedback through assistive technology.
Rendering and adding are separate tasks
Put this code in app.js beside the HTML file:
const topics = ['HTML', 'CSS'];
const form = document.querySelector('#topic-form');
const input = document.querySelector('#topic');
const list = document.querySelector('#topics');
const feedback = document.querySelector('#feedback');
function renderTopics() {
list.replaceChildren();
for (const topic of topics) {
const item = document.createElement('li');
item.textContent = topic;
list.append(item);
}
}
function addTopic(event) {
event.preventDefault();
const topic = input.value.trim();
if (topic === '') {
feedback.textContent = 'Önce bir konu yaz.';
return;
}
topics.push(topic);
renderTopics();
input.value = '';
feedback.textContent = topic + ' eklendi.';
input.focus();
}
form.addEventListener('submit', addTopic);
renderTopics();
The selectors find the elements by ID. The topics array holds our data; the ul is where we display it. Keeping these separate makes the next operations easier to follow.
RenderTopics first removes the old list contents with replaceChildren. For each topic it creates an li, sets its textContent and appends it. Without clearing first, drawing the whole array again would repeatedly duplicate the visible entries.
Supply the function for later
form.addEventListener('submit', addTopic) registers a handler for submission. We supply the function rather than immediately calling it. The browser passes the event information when submission occurs.
PreventDefault stops the form's normal navigation in this example. Input.value reads the field. Trim removes surrounding whitespace, not spaces within a topic name. If nothing remains, we write feedback and return before adding anything.
Otherwise we push the topic, redraw the list, clear the field and return focus to it. TextContent inserts user input as text. Typing <strong>HTML</strong> therefore shows those characters rather than interpreting them as markup.
Trace what happens on submission
The browser first applies the form's native behavior. An entirely empty required field can block submission. Whitespace can satisfy required, so our trim check answers an additional question.
When the submit event reaches the registered listener, the browser calls addTopic with event information. PreventDefault cancels the normal navigation for this exercise. Only then do we read the field's current value. Reading once at startup would miss later typing.
A valid topic is pushed, the list is rendered, the input cleared and focus restored. The same function runs again for a later event with a new current value. Defining, registering and invoking it are different stages.
Why listen to submit rather than only click?
Submission represents the form action, including keyboard use. A button-only click handler can introduce unnecessary separate flows. That is why we test Enter as well as the button.
PreventDefault does not stop event propagation. StopPropagation serves another purpose and is unnecessary here. Adding every available event method would obscure rather than improve this example.
The array and visible list are separate
Pushing into topics does not automatically create a li. RenderTopics synchronizes the view. Removing the initial render call can leave an empty visible list despite two existing array values.
Temporarily remove replaceChildren and add several topics. Repeated visible entries can result from redraw logic rather than duplicated data. Inspect the array length and DOM item count separately.
Why textContent rather than innerHTML?
The input is a topic name, not an HTML template. TextContent inserts characters as text, whereas innerHTML can parse markup. Enter <strong>HTML</strong> to make the difference visible. TextContent fits the data we actually want.
The final renderTopics() is an immediate call. The addTopic supplied to the listener is a function value for later invocation. Writing addTopic() there tries to run it during registration, before the browser supplies the submission event. The call/callback distinction now controls a real user interaction.
Test three behaviors
Add Linux and check for three entries. Submit only spaces and confirm that the array does not grow and feedback appears. Finally refresh: Linux disappears because the added data existed only in the current page's memory.
This is expected. We have not added localStorage or a server save. The next section will examine DOM operations and events in smaller experiments before adding persistence.
References: Reference 1 · Reference 2 · Reference 3