prop() method in jQuery

prop() in jQuery

Get or Set the value of a property for the first element in the set of matched elements.

Syntax 1 - Get the value of the property for the first element in the set of matched elements.

$(selector).prop(propertyName)

  • propertyName - The name of the property.
Syntax 2 - Set one property for the set of matched elements.

$(selector).prop(propertyName,propertyValue)

  • propertyName - The name of the property.
  • propertyValue - A value to set for the property. If null, the specified property will be removed.
Syntax 3 - Set more attributes for the set of matched elements.

$(selector).prop({propertyName1:value1,...propertyNamen:valuen)

  • propertyName - The name of the property.
  • value - A value to set for the property. If null, the specified property will be removed.
Syntax 4

$(selector).prop(propertyName,function(index,currentValue))

  • propertyName - The name of the property.
  • index - index position of the element in the set.
  • currentAttributeValue - The old property value as arguments.

Example 1 - Get checkbox value

Try Yourself

Example 2 - Get radio button value

Try Yourself

Example 3 - set/create many attribute values

Try Yourself

Example 4 - Get old value and set new value

Try Yourself

Example 5 - Difference between attr() and prop()

Try Yourself