Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What should I do in this case - getelementsbyclassname not working

Asked by Antony Bence Oct 11, 2022 1.1K views 1 answer
Share

About this question

Im struggling to figure out why my code is not working. Here is part of the JS:


function init() {
    var showMenu = document.getElementsByClassName('showMenu'),
        perspectiveWrapper = document.getElementById( 'perspective' ),
        container = perspectiveWrapper.querySelector( '.container' ),
        contentWrapper = container.querySelector( '.wrapper' );
    showMenu.addEventListener( click event, function( ev ) {
        ev.stopPropagation();
        ev.preventDefault();
        doc scroll = scrollY();
        // change top of contentWrapper
        contentWrapper.style.top = doc scroll * -1 + 'px';
        // mac chrome issue:
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        // add modal view class
        classie.add( perspectiveWrapper, 'modalview' );
        // animate..
        setTimeout( function() { classie.add( perspectiveWrapper, 'animate' ); }, 25 );
    });
Here is part of the HTML:

    

Company


    



    

Company


    

For some reason when I load the page, I get this error:
TypeError: undefined is not a function (evaluating 'showMenu.addEventListener')
I don't understand this because if I change this line:
var showMenu = document.getElementsByClassName('showMenu'),
to:
var showMenu = document.getElementById( 'showMenu' ),

It does work!

Why won't the class selector work but the id one will?

Your answer

1 Answer

More Java discussions