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
Release: 2024-12-27 22:07:10
Original
785 people have browsed it

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');
Copy after login
  • addClass(): Adds a class to the existing class list.
$("#td_id").addClass('newClass');
Copy after login
  • toggleClass(): Toggles between two classes, effectively swapping them.
$("#td_id").toggleClass('change_me newClass');
Copy after login

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!

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