1

I'm trying to create multiple sections and show or hide them on click. So I have three id's: section2, section2hidden, and section2visible.

Please take a look at the non-working code at this website. Click on the language arts link under 6th grade and then hit the back button in the top left corner (not on your browser!). You will see that it brings the element back, but it is still opacity:0 or something.

T. Junghans
  • 11,385
  • 7
  • 52
  • 75
Thomas Lai
  • 317
  • 1
  • 6
  • 15
  • 3
    Links come and go. Please provide a reproducible code example. – Brad Dec 09 '12 at 05:10
  • 3
    ids identify elements. classes style them. You should not be changing ids at all. – Dennis Dec 09 '12 at 05:16
  • @Brad +1 , that should be said more often! – d-_-b Dec 09 '12 at 05:17
  • @Dennis not everybody drinks the same CSS Kool Aid. Componentized CSS is a smart idea provided you don't over-granularize it but it doesn't render IDs useless or destructive for reducing bloat, which is the real enemy, not specificity which should be used as appropriate to keep things more, not less concise. – Erik Reppen Dec 09 '12 at 05:50

3 Answers3

2

Your code seems to be stalling while looking for a div that doesn't exist.

document.getElementById('6-2')

causes an error of cannot read property 'style' of null.

qooplmao
  • 17,622
  • 2
  • 44
  • 69
1

First of all, when you develop javascript code, you need to have a method to debug. If you are using Firefox, there's an addon called Firebug. If you are using Chrome, it's already integrated. Using that, you would see your "back" function fails to execute completely, because there is no element with "6-2" id, and you are trying to access properties of a null object. That answers "why it's failing" question. I'd recommend though to look up on these things:

  • Basic function concepts. You can have one function with parameters, instead of building "one(), two(), three()" etc functions.
  • Basic loop programming. You are using code like
    
    .getElementById('6-1')...
    .getElementById('6-2')...
    
    Use loops to let the machine work for you, don't just copy/paste blindly.
  • CSS classes. Instead of changing the id of the element, just add a class to it, eg "hidden". Then add a rule like
    .hidden{display:none;}
    When you take away the class, the element will reappear because the rule will no longer apply.
pkExec
  • 1,752
  • 1
  • 20
  • 39
0

When you press the back button, the initial section remains with the ID section2hidden. It should be changed back to section2.

rdiazv
  • 1,153
  • 7
  • 7