Hide element by class in pure Javascript [duplicate]
Solution 1:
document.getElementsByClassName
returns an HTMLCollection
(an array-like object) of all elements matching the class name. The style
property is defined for Element
not for HTMLCollection
. You should access the first element using the bracket(subscript) notation.
document.getElementsByClassName('appBanner')[0].style.visibility = 'hidden';
Updated jsFiddle
To change the style rules of all elements matching the class, using the Selectors API:
[].forEach.call(document.querySelectorAll('.appBanner'), function (el) {
el.style.visibility = 'hidden';
});
If for...of
is available:
for (let el of document.querySelectorAll('.appBanner')) el.style.visibility = 'hidden';
Solution 2:
var appBanners = document.getElementsByClassName('appBanner');
for (var i = 0; i < appBanners.length; i ++) {
appBanners[i].style.display = 'none';
}
JSFiddle.