WhiskerBiscuit
WhiskerBiscuit

Reputation: 5157

How can I change the class of an element with jQuery?

In my ASP.NET app I'm using AJAX and some web services to update an unknown number of instantiated user controls (hence the reason I'm using FindControl below).

When a function returns a result, I'm trying to change the color of a label control. This works:

$get("<%= me.FindControl("lblName").ClientID %>").style.color = 'red';

Now I would like to be able to specify the colors from a CSS file.

.MyRed 
{
    color:Maroon;
}

And here is the part I can't figure out. I've tried using addClass and toggleClass but the browser reports "Object doesn't support property or method 'toggleClass'" etc.

$get("<%= me.FindControl("lblName").ClientID %>")......  = '.MyRed';

As always, thanks in advance!

Upvotes: 1

Views: 161

Answers (6)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263047

Since you appear to be using the ASP.NET AJAX library, you can call Sys.UI.DomElement.addCssClass():

Sys.UI.DomElement.addCssClass(
    $get("<%= me.FindControl("lblName").ClientID %>"), "MyRed");

Upvotes: 2

$get is Microsoft specific but returns a DOM object..

So you can use this DOM object with jQuery like this:

$($get("<%= me.FindControl("lblName").ClientID %>")).toggleClass('MyRed');

Hope it helps...

Upvotes: 2

bevacqua
bevacqua

Reputation: 48516

what is $get? if you're trying to use jQuery you need to either use $ or the jQuery object.

$('#<%= me.FindControl("lblName").ClientID %>').removeClass("removeMe").addClass("addMe")

Upvotes: 1

Icarus
Icarus

Reputation: 63964

if you want to do that with jQuery, you need this instead:

$("#<%=lblName.ClientID %>").addClass("MyRed");

Upvotes: 1

pollirrata
pollirrata

Reputation: 5286

if you are using the id on the selector, befor the id value you shall add the "#" char

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190976

$('#<%= me.FindControl("lblName").ClientID %>').addClass('myRed');

Upvotes: 1

Related Questions