I have some problems.
1. First one witch is the simplest one. I have some buttons and I want to add a class when they are active and remove it from the others but for some reason is not working.
Code:
[code]const portfolioButtons = document.querySelectorAll(‘[data-content-target]’);
portfolioButtons.forEach(button => {
button.addEventListener(‘click’, ()=> {
const targetContent = document.querySelector(button.dataset.contentTarget);
const portfolioContent = document.querySelectorAll(‘[data-portfolio-content]’);
if(portfolioContent.classList === ‘containers-active’) {return};
portfolioContent.forEach(container => container.classList.remove(‘containers-active’))
targetContent.classList.add(‘containers-active’);
button.forEach(li => li.classList.remove(‘portfolio__button-active’));
button.classList.add(‘portfolio__button-active’);
});
});
2. This one is a bit tricky and I hope that I can explain it in such a way that you can understand.
I have a section of the site that has a different style for mobile and desktop. The thing is that I also want different functionalities because on mobile there are some weird interactions if I add the desktop functionality.
The desktop code is the first code you see in this post and THIS is the mobile code.
[code]
const portfolioButtons = document.querySelectorAll(‘[data-content-target]’);
portfolioButtons.forEach(button => {
button.addEventListener(‘click’, ()=> {
const targetContent = document.querySelector(button.dataset.contentTarget);
targetContent.classList.toggle(‘containers-active’);
button.classList.toggle(‘portfolio__button-active’);
});
});
3. And the last one. I want the active class to be on the first element by default on the desktop version. The thing is that if I add it in HTML as I would normally do it will be active on the mobile too. If I will make different classes and use media queries then the javaScript won’t work.
Here is:
Website –
Code Files –
1 way I could fix all of this is by using different data sets and different classes for both mobile and desktop and keep both codes in JS but I don’t know if it’s worth it.