On a quick glance I see 5 problems in your code:
element.className
is a String.
You can add a class to it with element.className += " hidden"
. Note the space before the "hidden" word.
Without the space you will get className = "contact_form_tophidden" (one word = one class) instead of "contact_form_top hidden" (two classes)
- Also you can't subtract a string by using
-=
, subtraction is for numbers only.
Instead of manipulating the String className, I suggest you use classList
which is an array-like list of classes that you can add
and remove
. If you want to be backward compatible with old browsers its best to use a framework such as jQuery or follow the className manipulation techniques described in https://stackoverflow.com/a/196038/519995
Also you need to use getElementsByClassName
(uppercase C)
getElementsByClassName
returns a collection of elements, you need to iterate them with a for
loop and modify the class of each, one at a time. Again, if you'd use a framework such as jQuery this would be much easier.
Also the if
statement you are using will always enter the first part and never the second part, since the content
is always "closed" when the function runs.