The error message "TypeError: Cannot set properties of null (setting 'innerHTML')" means you're trying to modify the content of an HTML element that doesn't exist yet. This usually happens when you're trying to access an element using JavaScript before the HTML document has fully loaded.
Here's a breakdown of the problem and how to fix it:
Understanding the Error
innerHTML: This property allows you to get or set the HTML content within an element.
null: This indicates that the JavaScript code is trying to access an element that hasn't been found in the HTML document.
Possible Causes
Script Execution Order: Your JavaScript code is likely running before the <span class="turno"></span> element is available in the HTML.
Dynamic Content: If the <span> element is added to the page dynamically (e.g., using AJAX), your JavaScript code might be trying to access it before it's been added.
Solutions
Move the Script:
Place your JavaScript code at the end of the <body> section of your HTML document. This ensures the HTML is fully parsed before the script runs.
Wrap your JavaScript code in a function that waits for the DOM (Document Object Model) to be fully loaded.
javascript
document.addEventListener('DOMContentLoaded', function() {
// Your JavaScript code to manipulate the <span class="turno"> element goes here
const turnoElement = document.querySelector('.turno');
turnoElement.innerHTML = 'X'; // Example: Set the initial turn
});
Check for Typos:
Double-check that the class name "turno" is spelled correctly in both your HTML and JavaScript.
Debugging Tips
Console Logging: Use console.log() to check if the <span> element is found:
javascript
console.log(document.querySelector('.turno'));
Browser Developer Tools: Use your browser's developer tools (usually accessed by pressing F12) to inspect the HTML structure and see if the element is present.
Let me know if you have any more questions or need further assistance!