In the code below I need to toggle the text for the three buttons between "Show" and "A", "B",C" when clicked. With the current code, nothing happens when the buttons are clicked.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style>
.placeholder {
padding: 0 0 0 5px;
}
</style>
<script>
function mFunction() {
document.querySelectorAll('[data-placeholder]')].forEach( item => {
item.addEventListener('click', () => {
var x = item.querySelector('.placeholder');
x.innerHTML = x.innerHTML === "" ? item.dataset.placeholder : "";
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<p>
<button onclick="mFunction()" data-placeholder="A">Show<span class="placeholder"></span></button>
<p>
<button onclick="mFunction()" data-placeholder="B">Show<span class="placeholder"></span></button>
<p>
<button onclick="mFunction()" data-placeholder="C">Show<span class="placeholder"></span></button>
</body>
</html>
addEventListenerandonclick, not both. If you are adding an event listener, you should add it when the document loads, not when the button is clicked. By then you are already doing the thing that you are supposed to be listening for!