Tom
Tom

Reputation:

addClass/removeClass to a SubClass?

I am new to javascript and still learning the ropes. I am trying to use addClass to a subclass

I assumed that this would work:

jQuery('#wrow .text').addClass("error"); 
jQuery('#wrow .text').removeClass("error");

But it doesn't ? Little unsure how to do this to subclasses ? I am sure you gurus will help in a jiffy! :)

Edit: I am actually using

jQuery('#wrow_' + nameSpace + '.text').addClass("error");

but it isn't working?

Upvotes: 0

Views: 2076

Answers (4)

Andrew G. Johnson
Andrew G. Johnson

Reputation: 26993

jQuery('#wrow_' + nameSpace + '.text').addClass("error");

What is nameSpace? Will it actually contain a space? If not you may need a space on one or both sides

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105898

If this is your actual code

jQuery('#wrow_' + nameSpace + '.text').addClass("error");

Then I suspect you're missing a space

jQuery('#wrow_' + nameSpace + ' .text').addClass("error");
// put a space right here -----^

Upvotes: 3

stefita
stefita

Reputation: 1793

how does your html looks like. I'm just guessing that .text is child element of #wrow?

jQuery('#wrow > .text').addClass("error"); 
jQuery('#wrow > .text').removeClass("error");

Upvotes: 0

Michael Haren
Michael Haren

Reputation: 108306

Perhaps describe a little bit more as to what you're trying to accomplish?

$('#wrow .text').addClass("error"); 
$('#wrow .text').removeClass("error");

Will take any descendent of #wrow with the .text class and add the error class to those elements.

If you want to find the #wrow element when it also has the class "text", then it should look like this:

$('#wrow.text').addClass("error"); // no space in the selector

I don't think that's what you want either because you'd really only have one #wrow in the page (if you have more, you have another problem as IDs are supposed to be unique) so please clarify.

Upvotes: 1

Related Questions