I’m trying to learn about implementing Data Attributes according to an article (link in code). In the code below, when you click on the button, it is supposed to gray out a couple of TA numbers and leave the other two FT numbers black. However, it is only graying out one number. I can’t figure out why.
<html>
<head>
<meta charset="utf-8">
<title>Testing Data Attributes</title>
<style>
.btn {
border: 1px solid black;
display: block;
margin: 5px;
text-align: center;
vertical-align: middle;
width: 100px;
}
.btn__text, p, a {
font-family: helvetica, sans serif; color: #000;
}
.btn--med {
padding: 5px;
}
</style>
</head>
<!-- **from https://webdesignerhut.com/html5-custom-data-attribute/**
-->
<body>
<h1>Sort Tools</h1>
<p>Button shows which items are brand FT.</p>
<button id="ft" class="btn btn__text btn--med">FT</button>
<button id="refresh" class="btn btn__text btn--med">refresh</button>
<p id="tool" data-brand="ta">TA 1495</p>
<p id="tool" data-brand="ft">FT 1496</p>
<p id="tool" data-brand="ta">TA 1497</p>
<p id="tool" data-brand="ft">FT 1498</p>
</body>
<script>
function buttonShowFt() {
var idtool = document.getElementById("tool");
idtool.style.color = '#000'; // reset color to black
var databrand = idtool.dataset.brand;
for (var i=0; i<4; i++) {
if (databrand === 'ta') {
idtool.style.color = '#ccc';
}
};
}
function buttonRefresh() {
var idtool = document.getElementById("tool");
idtool.style.color = '#000'; // reset color to black
}
document.getElementById("ft").addEventListener('click', buttonShowFt);
document.getElementById("refresh").addEventListener('click', buttonRefresh);
</script>
</html>