You can set a handler on the elements of the list and, when you hover the cursor, look for the closest parent element corresponding to the specified selector ( in your case, the class "block" )
function closest(el, sel) { if (el !== null) { return el.matches(sel) ? el : (el.querySelector(sel) || closest(el.parentNode, sel)); } } function toggleBg(e) { var parentBlock = closest(this, '.block'); parentBlock.style.backgroundColor = e.type === 'mouseenter' ? '#ff0' : ''; } [].forEach.call(document.querySelectorAll('.list'), function(li) { li.addEventListener('mouseenter', toggleBg, false); li.addEventListener('mouseleave', toggleBg, false); });
<div class="block"> <div> <ul> <li class="list">Item 1</li> <li class="list">Item 2</li> </ul> </div> </div>
And you can go from the reverse. That is, set the handler on the parent element, which we will "repaint" the background color and check on which element the event worked. If this element contains the class "list", then change the background color.
function toggleBg(e) { if (!e.target.classList.contains('list')) { return false; } this.style.backgroundColor = e.type === 'mouseover' ? '#ff0' : ''; } [].forEach.call(document.querySelectorAll('.block'), function(el) { el.addEventListener('mouseover', toggleBg, false); el.addEventListener('mouseout', toggleBg, false); });
<div class="block"> <div> <ul> <li class="list">Item 1</li> <li class="list">Item 2</li> </ul> </div> </div>