Menu

Cannot set properties of undefined (setting 'display')

In a scenario, I was trying to hide some HTML elements from the DOM at the runtime using the plain Javascript display property. When I was running the below JavaScript code to set the display=none for a particular element I was getting the error "Cannot set properties of undefined (setting 'display')".


document.getElementsByClassName("Feedback").style.display = "none";


This error I was seeing because the getElementsByClassName function was returning a list and hence system was unable to set the property because of ambiguity between list items. So to fix this we have to specify the index number next to getElementsByClassName function like below to set the style display=none.


document.getElementsByClassName('Feedback')[0].style.display = 'none';


The above statement will hide the first occurrence of an element that has class "Feedback".

No comments:

Post a Comment