Witam!
Piszę program w JavaScript, i napotkałem na poniższy błąd:
Uncaught TypeError: Cannot read property 'style' of null
at HTMLButtonElement.<anonymous>
Wiem co on oznacza, że element, którego style chcę zmienić ma wartość "null". (Jeśli źle rozumiem, będę wdzięczny za poprawkę). Lecz nie rozumiem dlaczego ma wartość "null".
Poniżej kod:
<!--HTML-->
<tr><td class="firstCell" id="mainElement"><button type="button" id="firstButton">przycisk</button></td></tr>
Proszę zauważyć, że element "#btower1" ma element nadrzędny, który też ma id (mainElement).
const btn = document.querySelector("#firstButton");
btn.addEventListener("click", function() {
const cell = document.querySelector(firstButton.parentElement.id);
console.log(cell);// null
cell.style.backgroundColor = "#00FF00"; //Uncaught TypeError: Cannot read property 'style' of null
at HTMLButtonElement.<anonymous>
});
Z góry dziękuję za wszelką pomoc!