Merlin38 Merlin38 - 4 years ago 114
CSS Question

Hide Multiple Elements in html using javascript

i have a little problem in my html code with javascript. Because i'd like to show or hide multiple ID of elements.
The javascript that i use is this one

<Script type="text/javascript" language="JavaScript"><!--
function HideContent(d){
document.getElementById(d).style.display = "none";
}
function ShowContent(d){
document.getElementById(d).style.display = "block";
}
//--></script>


in my HTML code

<div class = "left" id="colsxmenu">
<ul>
<li><a href="javascript:HideContent('colsxmenu')"> ENGLISH</a>
<li><a href="javascript:HideContent('uniquename')">FRENCH</a>


Actually if i select ENGLISH it works good hiding the colsxmenu, but what i need is if i select FRENCH i would like to hide more than only 1 elements.

i tryed to put ('colsxmenu';'colsxmenu2') but it doesn't works.

Someone could help me?
Thanks in advance!
ps: sorry for my english!

Answer Source

You can pass an array of elements to your function

<div id="first">first</div>
<div id="second">second</div>
<a href="javascript:HideContent(['first','second'])"> ENGLISH</a>
<script>
  function HideContent(obj) {
    for (var i = 0; i < obj.length; i++) {
      document.getElementById([obj[i]]).style.display = 'none';
    }
  }
</script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download