Home >Web Front-end >CSS Tutorial >How to Change a TD Tag's Class Using jQuery?

How to Change a TD Tag's Class Using jQuery?

Linda Hamilton
Linda HamiltonOriginal
2024-12-27 22:07:10793browse

How to Change a TD Tag's Class Using jQuery?

jQuery: Changing a Class Name

Scenario:
Consider a webpage where you need to alter the class of a specific td tag, identified by its ID, from within another DOM element's click event.

Question:
How can you obtain the td tag's ID and modify its class using jQuery?

Answer:
jQuery provides several methods for manipulating class attributes:

  • attr(): Sets the class directly regardless of its previous value.
$("#td_id").attr('class', 'newClass');
  • addClass(): Adds a class to the existing class list.
$("#td_id").addClass('newClass');
  • toggleClass(): Toggles between two classes, effectively swapping them.
$("#td_id").toggleClass('change_me newClass');

Refer to the full list of jQuery class attribute methods for more options.

The above is the detailed content of How to Change a TD Tag's Class Using jQuery?. 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