The goal here is to completely get rid of onclick in HTML and execute the function in JS instead through EventListener
Here is the short code below:
HTML
<button id="Toggle" onClick="MovelistShowHide('movelist')">Toggle Movelist On/Off</button>
<div id="movelist">
Movelist content
</div>
CSS
#movelist{display:none; background-color: lime;}
JS
function MovelistShowHide(element) {
var srcElement = document.getElementById(element);
if (srcElement != null) {
if (srcElement.style.display == "block") {
srcElement.style.display = 'none';
}
else {
srcElement.style.display = 'block';
}
return false;
}
}
3
Answers
ANSWER
look at code
Since we don’t know how many buttons/text divs you might end up having, and you’re already using CSS, I would suggest switching to classes – one to define the style of the text div(s), and another one that defines
display: block
called "show".When the button is clicked you can use the
classList
on the button’snextElementSibling
(ie the text div) to toggle the "show" class on and off.