Home >Web Front-end >CSS Tutorial >How Can I Change a TD's Class Using jQuery Based on Its ID?

How Can I Change a TD's Class Using jQuery Based on Its ID?

Susan Sarandon
Susan SarandonOriginal
2024-12-13 15:38:15915browse

How Can I Change a TD's Class Using jQuery Based on Its ID?

jQuery Class Manipulation

Changing a TD's Class Based on Its ID

Problem:

Within a click event for another DOM element, you wish to alter the class of a TD element based on its unique ID ("td_id").

Solution with jQuery:

To set the class outright, use the .attr() method:

$("#td_id").attr('class', 'newClass');

To add a class without affecting existing ones, use .addClass():

$("#td_id").addClass('newClass');

For a quick class swap, try .toggleClass():

$("#td_id").toggleClass('change_me newClass');

Additional Class Manipulation Methods:

  • [jQuery Documentation: Class Attribute Methods](https://api.jquery.com/category/class-attribute/)

The above is the detailed content of How Can I Change a TD's Class Using jQuery Based on Its ID?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn