2

I'd like to show/hide multiple ID of elements.

I have a problem with the javascript in my HTML file.

Here's the javascript:

<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>

And my HTML:

<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 it should hide more than only 1 elements.

I tried to add ('colsxmenu';'colsxmenu2'), but that didn't works.

Moses Davidowitz
  • 982
  • 11
  • 28
Merlin38
  • 35
  • 1
  • 6

2 Answers2

1

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>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
0

Define your HideContent function to accept variable number of argument. It should be some thing like below to hide multiple elements.

<script>
  function HideContent() {
    if (arguments.length > 0){
      for(var i=0; i < arguments.length; i++{
        document.getElementById(arguments[i]).style.display = "none";
      }
    } 
  }
</script>

After that you can call it like

HideContent('colsxmenu');
HideContent('colsxmenu', 'uniqename');
Gaurav Soni
  • 111
  • 4