0

I have a simple jQuery function that adds a class (.dataerror) to various html elements if they are empty. First, I'm doing this:

$('.editable').each(function() { if (!$(this).html()) { showerror($(this)); } }); 

And a super-simplified version of the function looks like this:

function showerror(element) { $(element).addClass('dataerror'); } 

However, this DOES NOT add the class to the elements if they are empty.

If I change the function to:

function showerror(element){ $(element).hide(); } 

...then the elements ARE hidden as expected if they are empty.

Can anyone tell my why .addClass() DO NOT work on the element in this context, while other methods such as .hide() DO work?

Thx.

1
  • what element types are you adding the ".editable" class to? Commented Nov 10, 2013 at 20:49

2 Answers 2

1

Instead of

$(element).addClass('dataerror'); 

can u try

element.addClass('dataerror'); 
Sign up to request clarification or add additional context in comments.

8 Comments

Yep, you have that working as expected there, but I can't get it to work here. Some jQuery methods like .hide() work but I can't get .addClass() to work. Weird. I need to look into the rest of my code. :o)
what version of jquery u use? checking in which browser ?
1.10.2 and latest Chrome & Safari on Mavericks.
I'm also running a couple of jQuery plugins which may be screwing things up here.
after $(element).addClass('dataerror'); can u add alert(element.attr('class')); and check
|
1

Use showerror(this); instead of showerror($(this)) because you are wrapping the DOM element into a jquery object twice.

1 Comment

Yep, see the fiddle from @abhiklpm below. I must have something else going on although I'm not getting any js errors or conflicts. Thx.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.