Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set property 'display' of undefined

Tags:

javascript

css

I'm writting script to hide/show menu but I get some troubles.

    function displayMenu() { //var classMenu = event.target.className; //classMenu += 'Menu';     //document.getElementsByClassName(classMenu).style.display = 'block'; document.getElementsByClassName('btn-pageMenu').style.display = 'block';     } 

In comment what I want to do finally, but even if I try with static var it's not working. In the CSS :

    fieldset.toolsbox ul.btn-pageMenu {display:none;} 

I try like this too :

    .btn-pageMenu {display:none;} 

No more success. Anybody have a suggestion ? I'm learning JS and I not finding errors when I compare with other similar scripts.

Thanks for your help :)

like image 497
TikTaZ Avatar asked Aug 30 '12 10:08

TikTaZ


People also ask

Can not set properties of undefined?

The "Cannot set properties of undefined" error occurs when setting a property on an undefined value. To solve the error, conditionally check if the value is of the expected type (object or array) or has to be initialized before setting the property on it.

Can't access property display a style is undefined?

The "Cannot read property 'display' of undefined" error occurs when trying to set the display property on an undefined value. To solve the error, make sure you access the display property after accessing the style property on a valid DOM element.


1 Answers

document.getElementsByClassName('btn-pageMenu') delivers a nodeList. You should use: document.getElementsByClassName('btn-pageMenu')[0].style.display (if it's the first element from that list you want to change.

If you want to change style.display for all nodes loop through the list:

var elems = document.getElementsByClassName('btn-pageMenu'); for (var i=0;i<elems.length;i+=1){   elems[i].style.display = 'block'; } 

to be complete: if you use jquery it is as simple as:

​$('.btn-pageMenu').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​ 
like image 188
KooiInc Avatar answered Sep 19 '22 04:09

KooiInc