Connecting Tech Pros Worldwide Help | Site Map

Set read only check box

Familiar Sight
 
Join Date: Jan 2008
Posts: 198
#1: 2 Weeks Ago
I want to set read only for check box when I click submit button. But my code didn't work. This is my code java script code.
Expand|Select|Wrap|Line Numbers
  1. document.getElementsById('check')[0].disabled=true;
  2. document.getElementsById('check')[1].disabled=true;
  3. or 
  4. document.getElementById('check')[0].readOnly=true;
  5. document.getElementById('check')[1].readOnly=true;
  6.  
could someone help me? I'm using php and javascript.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,630
#2: 2 Weeks Ago

re: Set read only check box


Expand|Select|Wrap|Line Numbers
  1. document.getElementsById('check')[0]
should be undefined. getElementsById() is an invalid method call.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: 2 Weeks Ago

re: Set read only check box


...and
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('check')[0]
doesn't make sense because it returns a single element. You need to give your elements unique IDs or access them by name.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,630
#4: 2 Weeks Ago

re: Set read only check box


Quote:

Originally Posted by acoder View Post

...and

Expand|Select|Wrap|Line Numbers
  1. document.getElementById('check')[0]
doesn't make sense because it returns a single element.

additional info: that call would look for the property "0" of the element with the id "check" and because Elements usually* don’t have such a property, undefined is returned.

* except when you explicity define such a property
Reply