I have 3 divs as parent elements of a button and what I want is toggle <div data-status='isPlaying'>...</div>
between the 3 divs.
Below is the code for better understanding
// the button element
const play = document.querySelectorAll('.play');
Array.from(play).forEach((btn, i) => {
btn.addEventListener('click', function () {
this.parentNode.dataset.status = 'isPlaying'
})
btn.parentNode.dataset.status = 'notPlaying'
})
Please what's wrong with this code and why isn't it working... Thanks
Top comments (4)
Hi, just remove the
this
in youraddEventListener
part and change it tobtn
. And also one suggestion, if you just wanna toggle the status, add the attribute calledplayStatus
to the root div, which is like container to those three divs and simply add and remove your divs to that attribute, instead of changing the status on every div.Thanks for the feedback. i have tried changing
this
tobtn
and still doesn't work. What I mean is that I have 3div
elements and those elements have a<button class='play'>
each. And whenever a button is clicked it should indicate on the parent element which is thediv
that the button is playing a audio. And when i click on the secondbutton
fromdiv
number two, thedata-status='isPlaying'
should be on the seconddiv
and not the first one anymoreAlrighty, this is my markup,
And this is my script
Thank you so much...instead of using
button.parentNode.playStatus
i used it as a dataset so I can retrieve it later on...Thanks