I'd like to have multiple <div>
on a page, each with a <button>
that toggles the <div>
's visibility.
If found toggle show/hide div with button?, and modeled my attempt on it--this is for one <div>
toggled by one <button>
on the page.
You can see my attempt at https://jsfiddle.net/pjamesnorris25/8xhtfb1p/17/.
I thought wrapping the collection_button[count].onclick = function()
in a for
loop over all of the <button>
s would do the trick, but clicking each of the <button>
s results only in the last <div>
being toggled as the console.log
shows--no matter which <button>
is clicked the console shows that "p88c3-button is trying to toggle p88c3-div"
.
So to be clear, how can one write the code, using plain javascript, so that each time one clicks on the "reply - p88c1" button
, it toggles the visibility of "div - p88c1" and when one clicks on the "reply - p88c2" button
, it toggles the visibility of "div - p88c2" and so on?
The two non-jQuery solutions at How do I associate individual buttons with individual divs? do what I want, but they both rely on a DOM relationship between the <button>
and the <div>
, i.e., parentNode
and nextElementSibling
, that doesn't obtain in my case, and I don't see how to modify either to work for me.
Thanks in advance for whatever help you can give me.