100

I have tried the following code, but it doesn't work. Any idea where I have gone wrong?

document.getElementsByClassName('appBanner').style.visibility='hidden';
<div class="appBanner">appbanner</div>

Using jQuery or changing the HTML is not possible as I am using [self->webView stringByEvaluatingJavaScriptFromString:@""]; in Objective-C.

1
  • 3
    getElementsByClassName returns an array of elements. You need something like: document.getElementsByClassName('appBanner')[0].style.visibility='hidden'; Commented Aug 24, 2013 at 2:45

4 Answers 4

166

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';
Sign up to request clarification or add additional context in comments.

3 Comments

fedeetz/Atanas Korchev provide a more direct answer 24AUG2013. Preferred.
Note that this will only hide the first element with class name 'appBanner'
I would go for display instead of visibility
55
var appBanners = document.getElementsByClassName('appBanner');

for (var i = 0; i < appBanners.length; i ++) {
    appBanners[i].style.display = 'none';
}

JSFiddle.

5 Comments

I beginner on JS, but why you write , i; on first line? This used in for cycle? ( but we declare i in cycle later ) Or it small mistake? Or maybe its some "best practice"? Could you comment it, please.
@DenisSavenko it's simply declaring a second var.
@DenisSavenko - It's valid var syntax to declare multiple variables separated by commas, but in this case it is a mistake to then also declare i with var in the for loop - though this doesn't actually cause an error, it's just untidy.
cute answer bro...i like it
This solution worked for me (c.P.u1's did not when I wanted to hide all elements) - I just needed to change the part ...style.display = 'none' to ...style.visibility = 'hidden'.
-1
Array.filter( document.getElementsByClassName('appBanner'), function(elem){ elem.style.visibility = 'hidden'; });

Forked @http://jsfiddle.net/QVJXD/

2 Comments

FYI: Array.filter is not a JS function. Looks like this uses Mootools.
`Array.Filter' doesn't work with Javascript
-4
<script type="text/javascript">
        $(document).ready(function(){

                $('.appBanner').fadeOut('slow');

        });
    </script>

or

<script type="text/javascript">
        $(document).ready(function(){

                $('.appBanner').hide();

        });
    </script>

2 Comments

The OP does not mention jQuery
<script> function myFunction() { var x = document.getElementById("left"); if (x.style.display === "none") { x.style.display = "block"; document.getElementsByClassName('sj-vm-filter')[0].style.visibility = 'visible'; } else { x.style.display = "none"; document.getElementsByClassName('sj-vm-filter')[0].style.visibility = 'hidden'; } } </script>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.