I am trying to understand why my code is not working. Here is the JS part:
function init() {
var showMenu = document.getElementsByClassName('showMenu'),
perspectiveWrapper = document.getElementById( 'perspective' ),
container = perspectiveWrapper.querySelector( '.container' ),
contentWrapper = container.querySelector( '.wrapper' );
showMenu.addEventListener( clickevent, function( ev ) {
ev.stopPropagation();
ev.preventDefault();
docscroll = scrollY();
contentWrapper.style.top = docscroll * -1 + 'px';
document.body.scrollTop = document.documentElement.scrollTop = 0;
classie.add( perspectiveWrapper, 'modalview' );
setTimeout( function() { classie.add( perspectiveWrapper, 'animate' ); }, 25 );
});
}
Here is the HTML part:
<div id="topBar">
<h1>Company</h1>
<a href="#" class="entypo-menu showMenu"></a>
</div>
<div class="line"></div>
<div id="fixedBar">
<h1>Company</h1>
<a href="#" class="entypo-menu showMenu"></a>
</div>
For some reason, when I load the page, I get this error:
TypeError: undefined is not a function (evaluating 'showMenu.addEventListener')
I do not understand this because if I change this line:
var showMenu = document.getElementsByClassName('showMenu'),
at
var showMenu = document.getElementById( 'showMenu' ),
It works!
Why the class selector will not work, but id will? I am trying to get both references with a class showMenuto execute JS.
source
share