0

I want to use prop() to set the attribute of a checkbox to checked.

When I use prop("checked",true) I can visually see that the checkbox is checked, but if I look at the HTML there is no attribute value called checked.

For example:

<input type="checkbox" class="my_class" checked>

is what I would expect to see after using prop("checked", true). However, I don't see any change in the HTML code. I want to later reference whether or not the attribute checked is set, but since there is not attribute called checked in the HTML, then I'm unable to reference it.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Nancy Collier
  • 1,469
  • 4
  • 17
  • 21

2 Answers2

2

When using prop() you are changing the property and not the attribute, so the changes can't be seen in a DOM inspector, but it is for all intents and purposes changed.

To later see if the element is checked, you'd do:

$('.my_class').is(':checked')

which would return true when checked or false when unchecked, regardless of what you might see in a DOM inspector.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • The problem is that the checkboxes are inside a form, that when saved is set to ``display:none``. When I later go to view the form (by setting ``display:block``, none of the checkboxes are checked. Why is this? – Nancy Collier Apr 06 '13 at 19:31
  • @NancyCollier - That should work just fine -> [**FIDDLE**](http://jsfiddle.net/c5NNs/) ? – adeneo Apr 06 '13 at 19:37
-3

Is there a specific reason you wouldn't use attr() for this application?

If not, that's what you should be using instead of prop()

Nick Barone
  • 148
  • 3
  • 12
  • adeneo said it great in his comment: "When using prop() you are changing the property and not the attribute, so the changes can't be seen in a DOM inspector" – Nick Barone Apr 06 '13 at 19:25
  • Yes, and the property is really what you want to change, that's why `prop()` would be the preferred method for this, it's just a little confusing in the beginning to not see things change in the DOM inspector. – adeneo Apr 06 '13 at 19:26
  • I thought attr() was deprecated in version 1.9? – Nancy Collier Apr 06 '13 at 19:32
  • @NancyCollier - not for actual attributes, for properties maybe. – adeneo Apr 06 '13 at 19:33