I have this function:
var togView = document.querySelector('.toggle-view');
var list = document.querySelector('.pt-list');
togView.onclick = function() {
const isAdded = togView.classList.toggle('list');
list.classList.toggle('list', isAdded);
};
Which I'm using to toggle a class called .list
on and off on this little element called toggle-view
and on a sibling element called pt-list
<div class="toggle-view">
<span class="thumb-view">Thumbnail View</span> | <span class="list-view">List View</span>
</div>
<div class="pt-list"></div>
This worked fine until I added another <div class="toggle-view">
element. Now only the first iteration of the toggle-view
element works.
How could I get this function to work on both <div class="toggle-view">
elements independently without having to assign them an ID and duplicate the function?
I found a similar question on here from a few years ago and from what I understand, I need to use this
?, but I can't figure out how to apply that to what's happening in my function.
Any guidance would be appreciated!