Search code examples
javascriptundefineddisplayuncaught-exception

( Uncaught TypeError: Cannot set property 'display' of undefined


function open() {
document.getElementsByClassName("nav").style.display='flex';}

I have tried several times to display the items but can't resolve it ,I hope someone helps thanks in advance!!


Solution

  • document.getElementsByClassName("nav")

    The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName() on any element; it will return only elements which are descendants of the specified root element with the given class name(s).

    you probably need to do the following (assuming you only have one element with class name as nave):

    (document.getElementsByClassName("nav")[0]).style.display='flex';