On Sun, 25 Apr 2004 19:08:59 -0700, mscir
<ms***@access4less.com.net.org.uk> wrote:
Ritesh Shah wrote:
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.
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=s howvar;
}
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
--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)