Skip to the content.

$.fn.prop(name [, value])

Get a JavaScript property from the first element, or set one on every element. Properties differ from attributes — they live on the DOM node object, not on the rendered HTML — and reflect the current state of the element rather than the markup that created it.

Common examples include checked (vs the checked attribute), selected, disabled, value, tagName, nodeName, htmlFor (the for attribute), and className (the class attribute).

Signatures

prop(prop: string): unknown;
prop(prop: string, value: unknown | ((this: Element, index: number, currentValue: unknown) => unknown)): this;
prop(props: Record<string, unknown>): this;

Parameters

Returns

When getting, the property’s current value. When setting, the original Dabby collection for chaining.

Examples

import $ from "dabbyjs";
import "dabbyjs/attributes/prop/prop";

// Read a checkbox's current state (vs the markup attribute)
const isOn = $("#agree").prop("checked"); // boolean
import $ from "dabbyjs";
import "dabbyjs/attributes/prop/prop";

// Disable every form input
$("form input").prop("disabled", true);
import $ from "dabbyjs";
import "dabbyjs/attributes/prop/prop";

// Set multiple properties on each option
$("option").prop({ disabled: false, selected: false });

See also