Simulator88
Simulator88

Reputation: 637

Div style is undefined (Javascript)

I found this script in stackoverflow.

 function showhide(id){
        if (document.getElementById) {
          var divid = document.getElementById(id);
          var divs = document.getElementsByClassName("hideable");
          for(var div in divs) {
             div.style.display = "none";
          }
          divid.style.display = "block";
        } 
        return false;
       }

<a href="#" onclick="showhide('features');" >features</a>
<a href="#" onclick="showhide('design');"  >design</a>
<a href="#" onclick="showhide('create');" >create</a>

<div class="hideable" id="features">Div 1</div>
<div class="hideable" id="design">Div 2</div>
<div class="hideable" id="create">Div 3</div>

But it says, div.style undefined. Why? :)

Upvotes: 10

Views: 31280

Answers (6)

Artur
Artur

Reputation: 342

The days, with IE no longer supported by many webapps, using for ... of loop is exactly what you were looking for at the time:

function showhide(id) {
  if (document.getElementById) {
    let divid = document.getElementById(id);
    let divs = document.getElementsByClassName("hideable");
    for (let div of divs) {
        div.style.display = "none";
    }
    divid.style.display = "block";
  }
  return false;
}

Upvotes: 0

FARHAD AFSAR
FARHAD AFSAR

Reputation: 438

in javascript for(var div in divs) do not act like foreach. in this case 'div' is index. so the element should be: divs[div]

Upvotes: 0

flavian
flavian

Reputation: 28511

You should never use a for-in loop for that.

document.getElementsByClassName('someClass')

returns a NodeList, which doesn't inherit from Array.prototype, but it is similar in some aspects. It's a list of nodes, just like the name says. This means it has a length property and should only be accessed using:

var myElements = document.getElementsByClassName('yourClass');
for (var i = 0, ii = myElements.length; i < ii; i++) {
    console.dir(myElements[i].style);
};

And here is how you should actually hide an element.

/**
 * Shows or hides an element from the page. Hiding the element is done by
 * setting the display property to "none", removing the element from the
 * rendering hierarchy so it takes up no space. To show the element, the default
 * inherited display property is restored (defined either in stylesheets or by
 * the browser's default style rules.)
 *
 * Caveat 1: if the inherited display property for the element is set to "none"
 * by the stylesheets, that is the property that will be restored by a call to
 * showElement(), effectively toggling the display between "none" and "none".
 *
 * Caveat 2: if the element display style is set inline (by setting either
 * element.style.display or a style attribute in the HTML), a call to
 * showElement will clear that setting and defer to the inherited style in the
 * stylesheet.
 * @param {Element} el Element to show or hide.
 * @param {*} display True to render the element in its default style,
 * false to disable rendering the element.
 */
var showElement = function(el, display) {
  el.style.display = display ? '' : 'none';
};

var myElement = document.getElementById('someID');
showElement(myElement, false);// it should now be hidden.

Upvotes: 12

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382177

Replace

for(var div in divs) {

with

for(var i=0; i<divs.length;i++) {
   var div = divs[i];

divs, the result of getElementsByClassName, isn't really an array but a NodeList, an array-like object and you were iterating on its properties, not its elements.

Upvotes: 2

karaxuna
karaxuna

Reputation: 26940

for(var i = 0; i < divs.length; i++) {
         divs[i].style.display = "none";
      }

EDIT: for in loops are used to loop through object properties

Upvotes: 4

Konstantin Dinev
Konstantin Dinev

Reputation: 34905

Make sure all the elements in your for-in loop are DOM elements. It's a good practice to filter for-in loop with a hasOwnProperty():

      for(var div in divs) {
           if (divs.hasOwnProperty(div)) {
               if (div && div.style) {
                   div.style.display = "none";
               }
           }
      }

Upvotes: -2

Related Questions