interviews.dotnetthread.com

↑ Grab this Headline Animator

Tuesday, November 3, 2009

Solution: How to change css class for an element in jquery?

In order to change the CSS class applied to an element using jQuery we can use methods like
addClass() and .removeClass()


$("#divId").addClass("classname");

$("#divId").removeClass("classname");


But if we want to change or toggle CSS class between two classes during some events like button click we can use method toggleClass() like below.

Say on click we need to change class from "btn_submit" and "btn_submit_active" or vise versa
if element has class "btn_submit_active" toggle to use "btn_submit". here we use toggleClass() method twise.


$("#divId").toggleClass("btn_submit");
$("#divId").toggleClass("btn_submit_active");

Submit this story to DotNetKicks

4 comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles