if checkbox is checked then take url from html data-url and open it in new tab with window.open()

149 Views Asked by At
<section class="main-checkboxes-section">
<div class="select-popup-divs">
<input
id="check-1"
type="checkbox"
class="css-checkbox"
checked
data-url="https://www.websitename.com/" />
<label for="check-1"><span>FIRST</span></label>
</div>

<div class="select-popup-divs">
<input
id="check-2"
ype="checkbox"
class="css-checkbox"
checked
data-url="https://www.websitename.com/" />
<label for="check-2"><span>SECOND</span></label>
</div>
</section>
const fixedButton = document.querySelector('#fixed-open-id');

fixedButton.addEventListener('click', () => {
const allDivs = document.querySelectorAll('.select-popup-divs');

allDivs.forEach((div) => {
const closestCheckbox = div.closest('.css-checkbox');

if (closestCheckbox.checked == true) {
indow.open(`${closestCheckbox.dataset.url}`);
}
});
});

on click of that button I expected for each div that has checkbox checked to open link in that checkbox's data-url but console says

Cannot read properties of null (reading 'checked')

0

There are 0 best solutions below