Attributes API
attr.toggle()
Adds the passed attributes to the matched DOM elements if they are not already applied, and removes any of the passed attributes that are currently present.
-
Accepts arguments as strings, arrays of strings, and an optional boolean value (true or false):
ArgumentDescriptiontrueAdds attributes.falseRemoves attributes.
Each value is seen as a new argument. - Returns a value for a single matched DOM element and a single passed attribute: true if the passed attribute is applied, false otherwise.
Example:
$('input').attr.toggle('checked', 'disabled');
// Adds the attributes 'checked' and 'disabled' to the <input> element.
// undefined
$('input').attr.toggle('checked');
// Removes the attribute 'checked' from the <input> element.
// false
$('input').attr.toggle('checked');
// Adds the attribute 'checked' to the <input> element.
// true
$('input').attr.toggle('checked', true);
// Adds the attribute 'checked' to the <input> element.
// true
$('input').attr.toggle('checked', false);
// Removes the attribute 'checked' from the <input> element.
// false
$('input ?').attr.toggle('checked', 'disabled', true);
// Adds the attributes 'checked' and 'disabled' to all <input> elements.
// undefined
$('button ?').attr.toggle('checked', 'disabled');
// Adds the attributes 'checked' and 'disabled' to all <button> elements.
// undefined
$('button ?').attr.toggle(['checked', 'disabled'], false);
$('button ?').attr.toggle('checked, disabled', false);
$('button ?').attr.toggle('checked disabled', false);
// Removes the attributes 'checked' and 'disabled' from all <button> elements.
// undefined
$('input, button ?').attr.toggle('checked');
// Removes the attribute 'checked' from all <input> elements and adds it to all <button> elements.
// undefined