checked attribute | checked property (Internet Explorer)

Switch View :
ScriptFree
checked attribute | checked property

[This documentation is preliminary and is subject to change.]

Sets or retrieves the state of the check box or radio button.

HTML 4.01 Specification, Section 17.4

Syntax

HTML<element checked="p" ... >
JavaScript

p = object.checked

Property values

Type: Boolean

false

Default. Control is not selected.

true

Control is selected.

Standards information

Remarks

Check boxes that are not selected do not return their values when the form is submitted.

A user can select a radio button only if the button has a name. To clear a selected radio button, a user must select another button in the set.

Windows Internet Explorer 8 and later. In IE8 Standards mode, parsing operations on the checked content attribute always affect both the checked content attribute and defaultChecked Document Object Model (DOM) attribute. For example, removeAttribute('checked') sets both checked and defaultChecked to false. Similarly, setAttribute('checked', 'checked') sets both DOM attributes to true (as if the element was being re-parsed) For more information on IE8 mode, see Defining Document Compatibility.

Internet Explorer 8 and later. In IE8 mode, the defaultChecked DOM attribute reflects the value of the checked content attribute.

Examples

This example retrieves the checked property to fire an event.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/checked.htm


<head>
    <script>
    function checkthis()
    {
        if (oCheckbox.checked == true)
        {
            window.open("http://www.microsoft.com");
        }
    }
    </script>
</head>
<body>
    <p>Check here if you wish to go to Microsoft:
    <input id="oCheckbox" type="checkbox" onclick="checkthis()"></p>
</body>

See also

input type=checkbox
input type=radio
Reference
defaultChecked
Conceptual
Introduction to Forms

 

 

Build date: 3/8/2012

Community Content

marceepooNu
How to programmatically change a checkbox from checked to not checked, or vice versa

In order to programmatically change a checkbox from checked to not checked, or vice versa, you may need to make it into an object.  
To make the checkbox into an object, you need to iterate through "elements" using the "getElementsByTagName method" to find the name you gave your checkbox.
See e.g., http://msdn.microsoft.com/en-us/library/ms762212(v=VS.85).aspx

Here's some code I put together using the getElementsByTagName method

'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' BEGIN_Sub '*' 
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub subCopyTheArgFileToDir(strDirTargete_namChkBoxe)'(strDirTargete, namChkBoxe)
'
 'The following commented lines appear  as a checkbox input element in the body of an Hta I'm using.
 '  The checkbox's "onclick" event calls this sub, which copies a file and unchecks the checkbox.
 '
 '<span id=idSpanrow2_col2>
 ' <input type="checkbox" 
    '  name="namCheckBoxRow2_col2" 
    '  id=idCheckBoxRow2_col2 
    '  value="1"
    '  onclick="subCopyTheArgFileToDir('ItWorks_vbs,namCheckBoxRow2_col2')">
 '</span>
'
 Dim strDirTarget_namChkBox
 Dim strDirTarget
 Dim strNamChkBox
    
 strDirTarget_namChkBox = strDirTargete_namChkBoxe
 
 
Dim colChkElem
Set colChkElem = window.document.getElementsByTagName("input")  
Dim objChkBox
    
 Dim arChkBoxOnClickArgs
 'Split(expression[, delimiter[, count[, compare]]])
 arChkBoxOnClickArgs = Split(strDirTarget_namChkBox, ",", 2, vbTextCompare)
 
 strDirTarget = arChkBoxOnClickArgs(0)
 strNamChkBox = arChkBoxOnClickArgs(1)
 
 strMsgBoxMsg = strMsgSrcFileName & vbCrLf & _ 
  "Line No. 91  " & vbCrLf & _
  "strDirTarget  =  " & strDirTarget & vbCrLf & _
  "strNamChkBox  =  " & namChkBox & vbCrLf '& _
  '"XX  =  " & XX & vbCrLf & _
  '"XX  =  " & XX & vbCrLf & _
  '"XX  =  " & XX & vbCrLf & _
  '"XX  =  " & XX & vbCrLf & _
  '"XX  =  " & XX '& vbCrLf & _
 MsgBox strMsgBoxMsg
 Dim objFSO
 Set objFSO=CreateObject("Scripting.FileSystemObject")
 
 Select Case strDirTarget
 
     Case UCase("ItWorks_Hta")
           strDirTarget = strP_drv_HTA_ItWorksDir ' This is a public variable, containing a path to a folder on my server
 
    Case UCase("ItWorks_vbs")
           strDirTarget = strP_drv_VBS_ItWorksDir' This is a public variable, containing a path to a folder on my server
  
  Case Else 
  
 End Select ' 'Select Case strDirTarget
 
 If Len(strDirTarget) > 0 Then 
    If objFSO.FileExists(strFileInArgFULLName) Then
        objFSO.CopyFile strFileInArgFULLName, strDirTarget
    End If
End If 
 
 
 
 For Each objChkBox In colChkElem   
  If objChkBox.Type = "checkbox" Then
         'MsgBox "checkbox found"
         
         If objChkBox.checked Then 
            Msgbox objChkBox.value & vbCrLf & objChkBox.name

            If StrComp(strNamChkBox, objChkBox.name, vbTextCompare) = 0 Then ' 0 = identical matches
                objChkBox.checked=False ' This changes the setting from checked to not-checked
            End If '

      End If  'If objChkBox.checked Then 
   
  End If 'If objChkBox.Type = "checkbox" Then
  
Next 
 'namChkBox.checked=False
 
End Sub ' subCopyTheArgFileToDir(strArge)
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'* END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub '*' END_Sub 
'*''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''