DOM attribute, property, and CSS class manipulation methods for managing element state, appearance, and behavior.
Add, remove, toggle, and check for CSS classes on elements.
/**
* Add one or more CSS classes to elements
* @param classes - Space-separated CSS class names to add
* @returns The Cash collection for chaining
*/
addClass(classes: string): this;
/**
* Remove one or more CSS classes from elements
* @param classes - Space-separated CSS class names to remove, or omit to remove all
* @returns The Cash collection for chaining
*/
removeClass(classes?: string): this;
/**
* Toggle one or more CSS classes on elements
* @param classes - Space-separated CSS class names to toggle
* @param force - Optional boolean to force add (true) or remove (false)
* @returns The Cash collection for chaining
*/
toggleClass(classes: string, force?: boolean): this;
/**
* Check if any element has the specified CSS class
* @param cls - CSS class name to check for
* @returns True if any element has the class, false otherwise
*/
hasClass(cls: string): boolean;Usage Examples:
// Add single class
$('.button').addClass('active');
// Add multiple classes
$('.modal').addClass('open visible');
// Remove specific classes
$('.item').removeClass('selected highlighted');
// Remove all classes
$('.element').removeClass();
// Toggle class
$('.menu').toggleClass('collapsed');
// Force toggle (always add)
$('.element').toggleClass('active', true);
// Force toggle (always remove)
$('.element').toggleClass('active', false);
// Check for class
if ($('.button').hasClass('disabled')) {
return; // Don't process click
}
// Conditional class management
$('.items').each(function(index, item) {
$(item).toggleClass('odd', index % 2 === 1);
});Get, set, and remove HTML attributes on elements.
/**
* Get all attributes of the first element
* @returns undefined (legacy behavior)
*/
attr(): undefined;
/**
* Get the value of an attribute from the first element
* @param attrs - Attribute name to get
* @returns Attribute value or null if not set
*/
attr(attrs: string): string | null;
/**
* Set an attribute on all elements
* @param attrs - Attribute name to set
* @param value - Attribute value to set
* @returns The Cash collection for chaining
*/
attr(attrs: string, value: string): this;
/**
* Set multiple attributes on all elements
* @param attrs - Object mapping attribute names to values
* @returns The Cash collection for chaining
*/
attr(attrs: Record<string, string>): this;
/**
* Remove one or more attributes from all elements
* @param attrs - Space-separated attribute names to remove
* @returns The Cash collection for chaining
*/
removeAttr(attrs: string): this;Usage Examples:
// Get attribute value
const href = $('.link').attr('href');
const title = $('.image').attr('title');
// Set single attribute
$('.input').attr('placeholder', 'Enter text...');
$('.link').attr('target', '_blank');
// Set multiple attributes
$('.image').attr({
src: '/images/photo.jpg',
alt: 'A beautiful photo',
title: 'Click to view larger'
});
// Remove attributes
$('.element').removeAttr('disabled readonly');
$('.link').removeAttr('target');
// Conditional attribute setting
$('.external-link').each(function(index, link) {
if (link.hostname !== window.location.hostname) {
$(link).attr('target', '_blank').attr('rel', 'noopener');
}
});
// Form manipulation
$('.required-field').attr('required', 'required');
$('.form').find('input').attr('autocomplete', 'off');Get, set, and remove DOM properties on elements (different from attributes).
/**
* Get the value of a property from the first element
* @param prop - Property name to get
* @returns Property value
*/
prop(prop: string): any;
/**
* Set a property on all elements
* @param prop - Property name to set
* @param value - Property value to set
* @returns The Cash collection for chaining
*/
prop(prop: string, value: any): this;
/**
* Set multiple properties on all elements
* @param props - Object mapping property names to values
* @returns The Cash collection for chaining
*/
prop(props: Record<string, any>): this;
/**
* Remove a property from all elements
* @param prop - Property name to remove
* @returns The Cash collection for chaining
*/
removeProp(prop: string): this;Usage Examples:
// Get property values
const isChecked = $('.checkbox').prop('checked');
const isDisabled = $('.input').prop('disabled');
const tagName = $('.element').prop('tagName');
// Set single property
$('.checkbox').prop('checked', true);
$('.input').prop('disabled', false);
// Set multiple properties
$('.form-field').prop({
required: true,
readOnly: false,
tabIndex: 0
});
// Remove property
$('.element').removeProp('customProperty');
// Form control manipulation
$('.select-all').on('click', function() {
$('.item-checkbox').prop('checked', $(this).prop('checked'));
});
// Property vs attribute differences
$('.input').attr('value', 'initial'); // HTML attribute
$('.input').prop('value', 'current'); // Current DOM property
// Boolean properties
$('.toggle').prop('hidden', !$('.toggle').prop('hidden'));Use prop() for:
checked, disabled, selected, hiddenvalue (current input value), selectedIndextagName, nodeName, classNameUse attr() for:
id, class, src, href, data-*value (initial/default value)aria-*, role, custom attributesExamples:
// Checkbox state
$('.checkbox').prop('checked', true); // Set current state
$('.checkbox').attr('checked', 'checked'); // Set HTML attribute
// Input values
$('.input').prop('value', 'new text'); // Set current value
$('.input').attr('value', 'default'); // Set default value
// Disabled state
$('.button').prop('disabled', true); // Disable functionally
$('.button').attr('disabled', 'disabled'); // Add HTML attribute