user2406993
user2406993

Reputation: 257

Passing an argument to a function

Very simply I want to use the following code several times on my page for a number of 'boxes', so how can I pass an argument when it's called ie calling conceal(box1ID) would conceal box1ID and so on.....

function conceal() {      
        if(document.getElementById('box1ID').style.display=='block') {
          document.getElementById('box1ID').style.display='none';
        }
        return false;
    }  

function show() {
    if(document.getElementById('box1ID').style.display=='none') {
      document.getElementById('box1ID').style.display='block';
    }
    return false;
}

Upvotes: 1

Views: 125

Answers (5)

mplungjan
mplungjan

Reputation: 178421

Here I save some code

function showhide(id,show) {      
  document.getElementById(id).style.display=show?'block':'none';
  return false;
}  

usage inline (I assume you use inline due to the return false)

<a href="#" onclick="return showhide('box1ID',true)">Show</a>
<a href="#" onclick="return showhide('box1ID',false)">Hide</a>

To toggle use

function toggle(id) {      
  document.getElementById(id).style.display=document.getElementById(id).style.display=="block"?"none":"block";
  return false;
}  

usage inline (I assume you use inline due to the return false)

<a href="#" onclick="return toggle('box1ID')">Toggle</a>

Upvotes: 1

Alex Guerra
Alex Guerra

Reputation: 320

I'm not sure what you need. Is something like this?

function conceal(boxId) {      
        if(document.getElementById(boxId).style.display=='block') {
          document.getElementById(boxId).style.display='none';
        }
        return false;
    }  

function show(boxId) {
    if(document.getElementById(boxId).style.display=='none') {
      document.getElementById(boxId).style.display='block';
    }
    return false;
}




show('box1ID');
conceal('box1ID');

Upvotes: 1

<input type="Button" onclick="conceal(this.id)"/>

Javascript:

function conceal(buttonId) {      
    if(document.getElementById('+buttonId+').style.display=='block') {
      document.getElementById('+buttonId+').style.display='none';
    }
    return false;
}  

Upvotes: 0

Gintas K
Gintas K

Reputation: 1478

You mean like this?

function conceal(boxID) {      
    if(document.getElementById(boxID).style.display=='block') {
      document.getElementById(box1ID).style.display='none';
    }
    return false;
}  

and

function show(boxID) {
    if(document.getElementById(boxID).style.display=='none') {
      document.getElementById(boxID).style.display='block';
    }
    return false;
}

Upvotes: 1

Jamie Hutber
Jamie Hutber

Reputation: 28126

Its very simple, just write it and include it...

    function conceal(element) {      
        if(document.getElementById(element).style.display=='block') {
          document.getElementById(element).style.display='none';
        }
        return false;
    }  

    function show(element) {
        if(document.getElementById(element).style.display=='none') {
          document.getElementById(element).style.display='block';
        }
    return false;
    }

Call it like so:
conceal('box1ID');

Upvotes: 2

Related Questions