By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Checkbox group disable

P: 6
Hello JS Group

I have a few checkbox groups in my form . I would like to enable or disable them based on some values chosen in option boxes. I have the following code, the disableGroup function is a variation of enableGroup function from this example

in radio button :
Expand|Select|Wrap|Line Numbers
  1. onclick="javascript:disableSomething()"
and the disableSomething() and disableGroup are:

Expand|Select|Wrap|Line Numbers
  1. function disableSomthing() 
  2.         {
  3.         var name1 = 'checkboxgroup1';
  4.         var name2 = 'checkboxgroup2';
  5.         var formname = 'theFormName';
  6.  
  7.         disableGroup(name1,formname);
  8.         disableGroup(name2,formname);
  9.  
  10.         ..............     
  11.         }
  12.  
  13.          function disableGroup(name, form) {
  14.          var group = form.elements[name];
  15.          for(var i = 0, n = group.length; i < n; ++i) {
  16.          group[i].disabled = true;
  17.          }
  18.          }
But when I run the code I get error:

"elements is null or not an object".

I have tried to figure out what is wrong but failed.Could you please have a look at my code and advise ? I'd be grateful.

Regards
Vajra
Nov 23 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
You're just passing the form name to disableGroup whereas it expects a form object.

Try document.forms[form].elements...
Nov 23 '07 #2

P: 6
You're just passing the form name to disableGroup whereas it expects a form object.

Try document.forms[form].elements...

Many thanks for the answer , the method you recommended works. I also tried doing it with getElementsById (to omit form name) but nothing happens:

Expand|Select|Wrap|Line Numbers
  1. var group = document.getElementById(name);

It would be great if you could tell me how to make the above work as well.

Many thanks and regards
Vajra
Nov 23 '07 #3

acoder
Expert Mod 15k+
P: 16,027
getElementById only gets one element. Since checkboxes are a group of elements, you will need to use document.getElementsByName instead.
Nov 26 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.