How to use getelementbyid with functions

N

Nicholas

Hello,

I have a simple function

function hide()
{
if (document.getElementById("DIV1").style.visibility == 'hidden')
{document.getElementById("DIV1").style.visibility = 'visible';}
else
{document.getElementById("DIV1").style.visibility = 'hidden';}
}

How can i pass something else instead of "DIV1" useing it as a function
parameter?
 
L

Lee

Nicholas said:
Hello,

I have a simple function

function hide()
{
if (document.getElementById("DIV1").style.visibility == 'hidden')
{document.getElementById("DIV1").style.visibility = 'visible';}
else
{document.getElementById("DIV1").style.visibility = 'hidden';}
}

How can i pass something else instead of "DIV1" useing it as a function
parameter?

So your question is really "how do I pass an argument to a function?",
right?

function hide(name) {
var style=document.getElementById(name).style;
if (style.visibility == 'hidden')
{style.visibility = 'visible';}
else
{style.visibility = 'hidden';}
}

Then you can invoke your function as "hide('DIV1')" or "hide('DIV2')", etc.


--
 
E

Evertjan.

Lee wrote on 25 apr 2007 in comp.lang.javascript:
So your question is really "how do I pass an argument to a function?",
right?

function hide(name) {
var style=document.getElementById(name).style;
if (style.visibility == 'hidden')
{style.visibility = 'visible';}
else
{style.visibility = 'hidden';}
}

Playful variation:

function hide_or_show_toggle(id) {
var s;
(s = document.getElementById(id).style).visibility =
(s.visibility == 'hidden') ?'visible' :'hidden';
};
 
N

Nicholas

/cut

Tank you very very much, i done somethin similar and couldn't belive why it
didn't work but i used double quote marks insted of single :)

Thumbs UP!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top