I recently encountered an issue while working on a school webpage. I implemented some JavaScript code to show and hide certain elements upon click, but ran into trouble with external CSS properties not being recognized by the script.
function grabClassName(name) {
return document.getElementsByClassName(name);
}
var txt = grabClassName('txt');
function toggleDisplay(elementArray, index) {
if (elementArray[index].style.display == 'none') {
elementArray[index].style.display = 'block';
} else {
elementArray[index].style.display = 'none';
}
}
The target object with the class 'txt' had the display set to none in the CSS file, but the JS code didn't seem to acknowledge this setting.
Upon first click, the .txt object was not displayed as expected
It took a second click to finally reveal the object
Why isn't the 'display: none;' property from the external CSS file being honored?
Another issue I faced:
I have a div identified as 'content' that has a box-shadow property. When trying to modify it through JavaScript based on a link click in the navigation menu, I received the error "Cannot read property 'style' of null."
var n = document.getElementById("content");
function removeBoxShadow() {
n.style.boxShadow = '0px 0px 0px black';
}
Switching from id to class resolved the error, but caused some instability.