I have this popup in HTML: <div class='flex-column flex-center flex-align-center'…
- User
I have this popup in HTML:
I have this button in HTML: Warenkorb
And I have this JavaScript function: function popup() { const elements = document.querySelectorAll('.popup'); const popupElement = document.getElementById('popup'); const popupContentElement = document.getElementById('popup-content');
elements.forEach(element => { element.addEventListener('click', () => { console.log(element.getAttribute('data-popup-content')); popupContentElement.textContent = element.getAttribute('data-popup-content');
popupElement.style.display = 'flex'; const progressBar = document.createElement('div'); progressBar.classList.add('progress-bar'); popupElement.appendChild(progressBar); setTimeout(function () { popupElement.style.display = 'none'; progressBar.remove(); }, 4000); setTimeout(() => { progressBar.style.width = '100%'; }, 0); });}); }
This is not working and console.log(element.getAttribute('data-popup-content')); returns null.
- Assistant
It seems like the issue might be with how you are trying to access the data attribute in the JavaScript function. Instead of using element.getAttribute('data-popup-content'), you can access the data attribute directly using element.dataset.popupContent.
Try updating your JavaScript function as follows:
function popup() { const elements = document.querySelectorAll('.popup'); const popupElement = document.getElementById('popup'); const popupContentElement = document.getElementById('popup-content'); elements.forEach(element => { element.addEventListener('click', () => { console.log(element.dataset.popupContent); popupContentElement.textContent = element.dataset.popupContent; popupElement.style.display = 'flex'; const progressBar = document.createElement('div'); progressBar.classList.add('progress-bar'); popupElement.appendChild(progressBar); setTimeout(function () { popupElement.style.display = 'none'; progressBar.remove(); }, 4000); setTimeout(() => { progressBar.style.width = '100%'; }, 0); }); }); } popup();With this code, the data attribute should be accessed correctly and displayed in the popup content.
- User
It just returns undefined now.
- Assistant
It seems there might be an issue with how the data attributes are being set on the button element. It's possible that the attribute is not being set correctly or is missing. Let's make sure the data attribute is set properly on the button element.
Update your button element in HTML to include the data attribute like this:
<button class='popup' data-popup-content='Das Produkt wurde zum Warenkorb hinzugefügt.'>Warenkorb</button>Make sure the data-popup-content attribute is correctly set on the button element. Once you've updated the button element, try running the JavaScript function again. The console.log statement should now return the correct value of the data attribute.
- User
I already have it like that. Can you double check your response?
- Assistant
I apologize for the oversight. Let's revisit the JavaScript function and make sure the data attribute is being accessed correctly.
Since you are using a custom data attribute, you can access it using element.dataset.popupContent instead of element.getAttribute('data-popup-content'). The correct way to access the data attribute in your JavaScript function should be:
function popup() { const elements = document.querySelectorAll('.popup'); const popupElement = document.getElementById('popup'); const popupContentElement = document.getElementById('popup-content'); elements.forEach(element => { element.addEventListener('click', () => { console.log(element.dataset.popupContent); popupContentElement.textContent = element.dataset.popupContent; popupElement.style.display = 'flex'; const progressBar = document.createElement('div'); progressBar.classList.add('progress-bar'); popupElement.appendChild(progressBar); setTimeout(function () { popupElement.style.display = 'none'; progressBar.remove(); }, 4000); setTimeout(() => { progressBar.style.width = '100%'; }, 0); }); }); } popup();