Replce getElementByClassName
with getElementsByClassName
note that its plural(Elements
) with "s" at end and not singular(Element
)
the same class can be added to multiple objects hence, selecting by class name returns multiple items, not just one so it uses the plural form Elements
instead of Element as in case of getElementById()
as id must be unique and selecting by id will return only one DOM element
and, since the function getElementsByClassName
returns an array of elements elmnt.scrollIntoView()
won't work,
so you need to use the first element of the array as elmnt[0]
so you code will be
function myFunction() {
var elmnt = document.getElementsByClassName("filter-btn");
elmnt[0].scrollIntoView();
}
However, I would rather suggest using getElementById
if you want to select only one element and use as below(first give id IdGivenToTheAnchoreTag
to anchor tag)
function myFunction() {
var elmnt = document.getElementById("IdGivenToTheAnchoreTag");
elmnt.scrollIntoView();
}