PDA

View Full Version : Show/Hide function using checkbox



Leigh79
05-25-2007, 01:09 PM
Hi Guys

Could someone give me a hand?

I've got an asp form which will allow users to update their profile. At the bottom of the form there is a checkbox which depending on it's status will display some additional information for the user to fill out.

The checkbox's initial value will be generated from their user record from the database. From here I then want to be able to hide/show the additional fields depending on both the checkbox checked status and the db variable. This would give 6 different combinations:

db variable = 1, checkbox = NULL, additional fields = show
db variable = 0, checkbox = NULL, additional fields = hide
db variable = 1, checkbox = 1, additional fields = show
db variable = 0, checkbox = 0, additional fields = hide
db variable = 1, checkbox = 0, additional fields = hide
db variable = 0, checkbox = 1, additional fields = show

Any ideas on how to write this as a function?

Thanks in advance
Leigh

Bob90
05-25-2007, 05:00 PM
Maybe like this.



//obj is the HTML object
//db is a variable you pass
function additionalInfo(obj, db)
{
if( (obj.checked && db) || (obj.checked && !db) || (obj.checked == null && db))
{return true;}
else
{return false;}
}
Will return true for show, and false for hide.
In javascript using '==' means that 1==true and 0==false


<input type="checkbox" onchange="additionalInfo(this,dbvalue)">

mwinter
05-25-2007, 06:26 PM
I'm somewhat confused as a checkbox cannot be "null": it's either checked (and successful) or not.

Is there some reason why this cannot be a two-part form?