6

Is there a difference between these two ways of default checking a checkbox:

document.getElementById(checkboxId).defaultChecked = checked; 

vs

document.getElementById(checkboxId).checked = checked; 

3 Answers 3

10

defaultChecked is the default state, checked is the current state.

If you change defaultChecked and then press a <input type="reset"> then the checkbox should reset to the state specified in the defaultChecked property.

If you change checked, then the state will change immediately.

Sign up to request clarification or add additional context in comments.

Comments

1

The defaultChecked property returns the default value of the checked attribute. This property returns true if the checkbox is checked by default, otherwise it returns false. http://www.w3schools.com/jsref/prop_checkbox_defaultchecked.asp

The checked property sets or returns the checked state of a checkbox. http://www.w3schools.com/jsref/prop_checkbox_checked.asp

1 Comment

Please don't use W3Schools as a reference. It is dreadful. Even here it doesn't make mention that defaultChecked can be set (which is what the question is doing).
-2

i guess theres no difference if you use it as a setter. but if you use it as getter there will be difference.

becouse .defaultChecked as the name already telling you is checking the default value not the current one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.