I'm trying to add an event listener to all li elements in a ul. I currently have the code working for the first list item as a proof but I'm not sure how to expand this to the entire list. I've tried several things like delegate(), on() and an each() but I keep coming up empty. Any help or suggestions would be greatly appreciated
(function() {
var $body = document.body
, $menu_trigger = $body.getElementsByClassName('menu-trigger')[0]
, $menu_list_item = $body.getElementsByClassName('menu-list-item')[0];
// , $menu_list_items = $body.getElementsByClassName('menu-list-items');
if ( typeof $menu_list_item !== 'undefined' ) {
$menu_list_item.addEventListener('click', function() {
$body.className = ( $body.className == 'menu-active' )? '' : 'menu-active';
});
}
if ( typeof $menu_trigger !== 'undefined' ) {
$menu_trigger.addEventListener('click', function() {
$body.className = ( $body.className == 'menu-active' )? '' : 'menu-active';
});
}
}).call(this);
