Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I indicate that a checkbox should be checked?

Member
 
Join Date: Feb 2007
Posts: 95
#1: 3 Weeks Ago
I have a form named "FrontPage_Form2" and a checkbox named "checkboxPowerUnit"; and, I want to programmatically change whether or not it is checked by default based on a selection made in a selection box. I have tried:

Expand|Select|Wrap|Line Numbers
  1. document.FrontPage_Form2.checkboxPowerUnit.checked=true;
  2. document.all.checkboxPowerUnit.checked=true;
  3. document.form[0].checkboxPowerUnit.checked=true;
  4. document.form[0].checkboxPowerUnit[0].checked=true;
  5. document.FrontPage_Form2].checkboxPowerUnit[0].checked=true;
and every one results in an error saying "document.whatever" is null or not an object. Can anyone tell me how to indicate that this checkbox should be checked or unchecked?

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,167
#2: 3 Weeks Ago

re: How do I indicate that a checkbox should be checked?


Assign it an id and then use document.getElementById(idstring)
Member
 
Join Date: Feb 2007
Posts: 95
#3: 3 Weeks Ago

re: How do I indicate that a checkbox should be checked?


it has an id. the id and the name are the same.
Member
 
Join Date: Feb 2007
Posts: 95
#4: 3 Weeks Ago

re: How do I indicate that a checkbox should be checked?


Expand|Select|Wrap|Line Numbers
  1. document.getElementById('checkboxPowerUnit').checked=true;
  2. document.getElementById('checkboxPowerUnit').value="YES";
also do not work
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,167
#5: 3 Weeks Ago

re: How do I indicate that a checkbox should be checked?


Are you perhaps running this script before the page has loaded?
if document.getElementById('checkboxPowerUnit') is giving you a null object, then that object does not exist

http://www.w3schools.com/JS/tryit.as..._input_checked
Reply