K
K
kally2018-02-22 15:24:45
JavaScript
kally, 2018-02-22 15:24:45

Appearance and disappearance of a block having a class - Native JS and DOM?

Good afternoon. Why did getElementsById work but not class lookup?
Here is the code:

function Aaa(){
// блок исчезновения
      var links = document.getElementsByClassName('item');
      links.style.display = 'none';	  
}

function Bbb(){
// блок появления
      var links = document.getElementsByClassName('item');
      links.style.display = '';
}

            // После загрузки документа
    window.onload = function(){	 
  
        document.getElementsByClassName('aaa').onclick = function(){
        alert("блок исчезнет");
        Aaa();			
        }	
    
        document.getElementsByClassName('bbb').onclick = function(){
        alert("блок появится");
        Bbb();				   
        }  
  }

And another problem - even when it worked (the block disappeared / appeared, the alert popped up)
even then at the bottom of the page there was an inscription of the js code from the file file.js
Why is that? I already put it in the head and under the closing body

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2018-02-22
@delphinpro

and you look in the documentation what getElementById returns and what getElementsByClassName
I give a hint - getElement s ByClassName

S
Stimulate, 2018-02-22
@Stimulate

https://jsfiddle.net/du2ntonp/14/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question