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
Release: 2024-12-13 15:38:15
Original
914 people have browsed it

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');
Copy after login

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

$("#td_id").addClass('newClass');
Copy after login

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

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template