From 4675b369d6cd3568c939d34f7d12fadfcf9fcfc5 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Wed, 30 Jun 2021 16:05:45 -0700 Subject: [PATCH] fix: improve JavaScript selectors --- newspack-theme/js/src/amp-fallback.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/newspack-theme/js/src/amp-fallback.js b/newspack-theme/js/src/amp-fallback.js index 390a79cf0..d6385a4c4 100644 --- a/newspack-theme/js/src/amp-fallback.js +++ b/newspack-theme/js/src/amp-fallback.js @@ -40,7 +40,7 @@ // Menu toggle variables. const mobileToggle = document.getElementsByClassName( 'mobile-menu-toggle' ), - body = document.getElementsByTagName( 'body' )[ 0 ], + body = document.body, mobileSidebar = document.getElementById( 'mobile-sidebar-fallback' ), desktopToggle = document.getElementsByClassName( 'desktop-menu-toggle' ), desktopSidebar = document.getElementById( 'desktop-sidebar-fallback' ), @@ -92,8 +92,8 @@ // Mobile menu fallback. for ( let i = 0; i < mobileToggle.length; i++ ) { - const mobileOpenButton = headerContain.getElementsByClassName( 'mobile-menu-toggle' )[ 0 ], - mobileCloseButton = mobileSidebar.getElementsByClassName( 'mobile-menu-toggle' )[ 0 ]; + const mobileOpenButton = headerContain.querySelector( '.mobile-menu-toggle' ), + mobileCloseButton = mobileSidebar.querySelector( '.mobile-menu-toggle' ); mobileToggle[ i ].addEventListener( 'click', @@ -110,8 +110,8 @@ // Desktop menu (AKA slide-out sidebar) fallback. for ( let i = 0; i < desktopToggle.length; i++ ) { - const desktopOpenButton = headerContain.getElementsByClassName( 'desktop-menu-toggle' )[ 0 ], - desktopCloseButton = desktopSidebar.getElementsByClassName( 'desktop-menu-toggle' )[ 0 ]; + const desktopOpenButton = headerContain.querySelector( '.desktop-menu-toggle' ), + desktopCloseButton = desktopSidebar.querySelector( '.desktop-menu-toggle' ); desktopToggle[ i ].addEventListener( 'click', @@ -129,8 +129,8 @@ // 'Subpage' menu fallback. if ( 0 < subpageToggle.length ) { const subpageSidebar = document.getElementById( 'subpage-sidebar-fallback' ), - subpageOpenButton = headerContain.getElementsByClassName( 'subpage-toggle' )[ 0 ], - subpageCloseButton = subpageSidebar.getElementsByClassName( 'subpage-toggle' )[ 0 ]; + subpageOpenButton = headerContain.querySelector( '.subpage-toggle' ), + subpageCloseButton = subpageSidebar.querySelector( '.subpage-toggle' ); for ( let i = 0; i < subpageToggle.length; i++ ) { subpageToggle[ i ].addEventListener(