Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Properties

Besides attributes, there are also element properties — these include selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected. Well, the list isn't that long, you can memorize it. To work with properties we use methods from the prop() family:

get the property value

set the property value

set multiple values

using a callback function index is the element's position in the selection value is the current property value

remove a property (most likely, you'll never need this)

Let's see an example:

See how the system works without our interference — click the checkbox, the dropdown, try submitting the form.

Now let's run a series of experiments (don't forget to refresh the page):

  1. Check the checkbox using the attr() method:

  2. Now uncheck it with the mouse — the attr() value remained unchanged, while the prop() value changed

  3. Try checking it again using the attr() method

A brief explanation of what's going on. On the first call to attr("checked", "checked"), the checkbox gets checked because both the attribute and the checked property are changed. On subsequent calls, nothing happens — only the attribute value changes, and it's already checked.

Next experiment:

  1. Check the checkbox with the mouse

  2. Uncheck it — the attr() value doesn't change

  3. Try setting the value by calling:

What's interesting in this experiment is this: calling attr("checked", "checked") doesn't work after the user has changed the checkbox state

And one more experiment with the second checkbox:

  1. Remove the check:

  2. Set the check:

  3. Remove the check again using attr()

  4. Repeat until you drop

If it works — don't touch it, you'll break everything with the mouse :)

Compare this with the behavior of prop():

  1. Remove the check:

  2. Set the check:

  3. You can click the checkbox with the mouse and repeat the steps above in any order — everything will work like clockwork

I hope I've made it clear enough when to use attr() and when to use prop()

That's not all — we still have the disabled property! But don't worry, its behavior is more predictable since the user can't interfere with its state:

  1. Enable the radio button:

  2. Disable it:

  3. Repeat

Same behavior when using prop():

  1. Enable:

  2. Disable:

  3. Repeat

Well, you could use attr(), but no!

Last updated