I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
You haven’t posted your HTML code, but I assume you have only one .search-input ?
document.querySelector('.search-input) returns the first element that matches the selector.
document.getElementsByClassName returns a HTML collection (an array-like object) of ALL elements that match, even if it’s just one.
If you want to use getElementsByClassName, you’d have to access it by picking the first element of that list: document.getElementsByClassName(.search-input)[0]
Question is why you’d want to do that. querySelector is a newer feature in the language, getElementsByClassName almost seems like ancient syntax (to me, anyway).
the reason I want to use getElementsByClassName instead of querySelector is that I have a task to write code just in JS without using the jQuery library
I’m not sure but there might be some misunderstanding.
.querySelector is a feature of pure JavaScript, no jQuery needed.
.querySelector was included in JavaScript a couple of years ago, in order to offer the same convenience of jQuery’s way to select DOM elements - like $('.className').
Since .querySelector and .querySelectorAll have been included in pure JavaScript, there’s no need anymore for verbose syntax like .getElementsByClassName or .getElementById.