Thursday 30 June 2016

javascript - Get clicked element onclick

var submenus = document.getElementsByClassName("submenu");
for (var i = 0; i < submenus.length; i++) {

submenus[i].onclick = function() {
toggle(submenus[i].nextSibling);
return false;
}
}

function toggle(el) {
if (el.style.display == 'block') {
el.style.display = 'none';
} else {

el.style.display = 'block';
}
}


Causes error: TypeError: submenus[i] is undefined



I assume submenus[i] is not in the scope of the function. How do I get the element clicked so I can toggle it's next sibling?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...