0
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!!

1 Answers1

0

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';
Ritesh Waghela
  • 3,474
  • 2
  • 19
  • 25
  • This is correct. document.getElementsByClassName("nav") returns an array, you need to select the first element. But if the array is empty, the first element would be undefined and throw the same error. You'll need to check if style exists before checking display. – David Klinge Jul 12 '21 at 18:04
  • 1
    From [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer): "_avoid trying to answer questions which ... have already been asked and answered many times before._". This question is asked [on a daily basis](https://stackoverflow.com/questions/linked/10693845?sort=newest). Please flag to close them as a duplicate instead of answering them. – Ivar Jul 12 '21 at 18:04