Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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()

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top