0

I want to make a JS function that switch visible/hidden.

var foo = function(n){      
    var hidden_elements = document.getElementsByName('hidden');     
    for(var i=0;i<hidden_elements.length;i++){
        hidden_elements[i].style.visibility = 'hidden';
    }       
    hidden_elements[n].style.visibility = 'visible';
};

It works on Firefox and Chrome, but it doesn't on IE. Why? Thanks in advance.

Yugo Kamo
  • 2,349
  • 4
  • 18
  • 13
  • are you sure you want `visibility` and not `display`? The former means that the hidden element still occupies space in the DOM, but the latter does not. – Russ Cam Aug 23 '11 at 09:30
  • http://stackoverflow.com/questions/278719/getelementsbyname-in-ie7 maybe this will help – Igor Dymov Aug 23 '11 at 09:34
  • Does all elements in the form have the name as "hidden" ? How will u call this function foo() – AmGates Aug 23 '11 at 09:54
  • 1
    Reason #3178 why IE is the worst browser on the planet. – Rob Aug 23 '11 at 12:20

3 Answers3

1

I would recommend saving yourself the horror and going with:

The libraries do a lot to smooth over the surprises of different browsers. If you are being super minimalist you can always check the source for how they are handling the differences. Also have a look at quirksmode's compatibility listing.

I know I didn't give a solid answer but you are going to run into these troubles all the time and these are some good tools for hammering them out.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Tegra Detra
  • 24,551
  • 17
  • 53
  • 78
0

IE up to IE8 does not follow W3C specs. Microsoft has their own standards. Many scripting methods that work on Firefox or Chrome (which are W3C standards) may not work properly in various builds on IE.

Why don't you try something from scratch? Either that, or do some ease of access. You can do this by making a pattern for ids and dynamically building those ids (may be incremental). Then, access those tags from their id.

Access by name is not preferred. Id is most appropriate.

Kris
  • 8,680
  • 4
  • 39
  • 67
0

Your html is invalid. the "name" property needs to be unique. Use "class" instead.

Internet Explorer might give some issues, so DOM polyfills like flowjs could be used.

vasanth
  • 715
  • 10
  • 23
Raynos
  • 166,823
  • 56
  • 351
  • 396