Go Back   Dynamic Drive Forums > General Coding > JavaScript
Search Dynamic Drive Forums:

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-25-2007, 02:09 PM
Leigh79 Leigh79 is offline
New Comer (less than 5 posts)
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Show/Hide function using checkbox

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
Reply With Quote
  #2  
Old 05-25-2007, 06:00 PM
Bob90 Bob90 is offline
Regular Coders
 
Join Date: Feb 2007
Location: England
Posts: 254
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Maybe like this.

Code:
//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

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

Last edited by Bob90; 05-25-2007 at 06:06 PM.
Reply With Quote
  #3  
Old 05-25-2007, 07:26 PM
mwinter mwinter is offline
Elite Coders
 
Join Date: Dec 2004
Location: UK
Posts: 2,361
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
__________________
Mike
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:04 PM.

Home - Contact Us - Archives - Link to DD - Top 

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.