PDA

View Full Version : Collapsing show() and hide() functions into one?



Mistrel
05-15-2006, 05:55 AM
I'm using the following script to show/hide a particular element on my website. How would I collapse these two functions into one?

(if hidden, unhide. if visible, hide)


function show(){
document.getElementById("live").style.visibility="visible"
}


function hide(){
document.getElementById("live").style.visibility="hidden"
}

jscheuer1
05-15-2006, 06:43 AM
function showhide(){
var el=document.getElementById('live').style;
el.visibility=el.visibility=='hidden'? 'visible' : 'hidden';
}

Mistrel
05-15-2006, 07:41 AM
Thank you!! ^^

I wanted to use this for several elements so I modified it a bit. This is the final snippet:


function showhide(id){
var el=document.getElementById(id).style;
el.visibility=el.visibility=='hidden'? 'visible' : 'hidden';}
</script>