Help with must select at least one checkbox javascript function.

This is a discussion on "Help with must select at least one checkbox javascript function." within the JavaScript Forum section. This forum, and the thread "Help with must select at least one checkbox javascript function. are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 27th, 2007, 20:06
New Member
Join Date: Sep 2007
Location: USA
Age: 32
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Help with must select at least one checkbox javascript function.

Hi,

I have a dynamic form with multiple checkboxes. I need to make sure that at least one checkbox is checked but only when the checkbox is actually unchecked. For example, the user unchecks 4 out of 5 boxes but when he unchecks box 5 an alert appears that one box must be checked and box 5 is left checked until he checks another and then unchecks box 5.

I've written the following javascript function,but it is unchecking the check box after throwing the alert.I need to check the check box after throwing the alert.
HTML: Select all
function showElement(ID) {
var checkSelected = false;
for (i = 0; i < planForm.locationOptions.length; i++) {
if (planForm.locationOptions[i].checked) {
    checkSelected = true
    document.getElementById("refreshing").style.display="block";
}
}
if (!checkSelected) {
alert("At least ONE Location must be selected!");
checkSelected = true;
return false;
}
return true;
}
and calling this function under <html:multibox> onclick.
HTML: Select all
<html:multibox property="locationOptions" onclick="if(showElement('refreshing')){document.planForm.action='refreshPlanView.do';
                                                document.planForm.submit();}">
Please advise.

Thanks for the help.

Last edited by c010depunkk; Nov 28th, 2007 at 06:12. Reason: Please use [HTML] tags when posting HTML
Reply With Quote

  #2 (permalink)  
Old Nov 28th, 2007, 06:19
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Help with must select at least one checkbox javascript function.

Your code would only work if all the boxes are unchecked or if the last box is unchecked. Try this:
HTML: Select all
function showElement(ID) {
var num_checked=0;
for (i = 0; i < planForm.locationOptions.length; i++) {
  if (planForm.locationOptions[i].checked) {
    num_checked++;
  }
}
if(num_checked==0) {
   alert("At least ONE Location must be selected!");
   return false;
}
return true;
}
Reply With Quote
  #3 (permalink)  
Old Nov 28th, 2007, 18:15
New Member
Join Date: Sep 2007
Location: USA
Age: 32
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with must select at least one checkbox javascript function.

Hi,

Thanks for the code,but it is also doing the same thing.

I.e once the last check box is unchecked,it is throwing alert,but the checkbox is getting unselected.I need to keep the check box as selected after throwing alert and user clicking "Ok" on the alert.

Thanks
Reply With Quote
  #4 (permalink)  
Old Nov 28th, 2007, 18:50
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Help with must select at least one checkbox javascript function.

Not sure if this will work, but try this:
HTML: Select all
function showElement(ID) {
var num_checked=0;
var last_checked=0;
for (i = 0; i < planForm.locationOptions.length; i++) {
  if (planForm.locationOptions[i].checked) {
    num_checked++;
    last_checked=i;
  }
}
if(num_checked==0) {
   alert("At least ONE Location must be selected!");
   planForm.locationOptions[last_checked].checked=true;
   return false;
}
return true;
}
Reply With Quote
  #5 (permalink)  
Old Nov 28th, 2007, 19:28
New Member
Join Date: Sep 2007
Location: USA
Age: 32
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Help with must select at least one checkbox javascript function.

Hi,

Thanks for the input,but once the Alert is thrown it is checking the check box with value=0 as last_checked=0 has been declared,it is not checking the last unchecked check box.

Please advise..

Thanks
Reply With Quote
  #6 (permalink)  
Old Nov 29th, 2007, 05:39
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Help with must select at least one checkbox javascript function.

That's true, that was a logic error on my part....

I think a feasible solution would be to declare a global variable 'last_checked' and every time a check box is checked (in the onclick eventf), set this global variable to the value of that box. Then when the last one is unchecked, you could re-check the box who's value is stored in the global variable....
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript show/hide function danny322 JavaScript Forum 1 May 15th, 2008 12:46
add image src from javascript function ktsirig JavaScript Forum 1 Mar 14th, 2008 11:38
Regarding select boxes in javascript bhanu Introduce Yourself 5 May 19th, 2007 12:45
Select & View Checkbox ktsirig PHP Forum 0 Jan 23rd, 2006 11:13
help with javascript function kinjiro JavaScript Forum 1 Nov 21st, 2003 15:58


All times are GMT. The time now is 19:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43