Hidden textarea

R

Ritesh Shah

Is there anyway in JavaScript to hide/show a textarea based on what radio
button is selected? I am totally new to JavaScript and have no idea about
any of it.
Thanks.
 
M

Michael Winter

How about: put it inside a div and set div visibility, this works in my
IE6, Netscape 7.1, Firefox 0.8:

Why use a DIV? You're adding structure where there is none. The TEXTAREA
can be hidden directly. Furthermore, using the visibility property isn't
always appropriate: hidden elements still take up space. Depending on the
structure and layout of the OP's document, use of the display property
might be better.
function setdivvisibility(show) {
if (show)
showvar="visible";
else
showvar="hidden";

The variable, showvar, is now global. Use the var keyword to keep it local.
document.getElementById('div1').style.visibility=showvar;
}

It would be a good idea to code defensively. I notice that you regularly
use methods, object and return values without checking for support. This
produces code that might fail and produce needless errors. Even
applications in C++, with a known environment, should be written
defensively, and more so for browser scripting.

// Make sure that document.getElementById() is meaningful
if( !document.getElementById ) {
if( document.all ) {
document.getElementById = function( id ) {
return document.all[ id ];
};
} else {
document.getElementById = function() {
return null;
};
}
}

function setVisibility( id, show ) {
var obj = document.getElementById( id );

if( obj && obj.style ) {
// Use an empty string to return to the default value.
obj.style.visibility = show ? '' : hidden;

/*
* Alternatively:
* obj.style.display = show ? '' : 'none';
*/
}
}

The call should now be modified to pass the id attribute value of the
TEXTAREA to be hidden.

[snip]

Mike
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top