user007
user007

Reputation: 3243

jQuery - each() on cloned element

I am trying to do a each() on a cloned element,

var html = $(this).clone().html();

html.find('.class').each(function () {
    $(this).removeClass('class-to-remove');             
});

console.log(html);

but when I see var html in console then it shows the previous value, not the value after each() was done.

Please tell me how to get var where the each() was done.

Upvotes: 2

Views: 1630

Answers (1)

Pointy
Pointy

Reputation: 413737

The return value of .html() is a string. You're better off not calling it at all in this case; just use the return value from .clone().

var cloned = $(this).clone();
cloned.find('.class').each(function() {
  $(this).removeClass('whatever');
});

console.log(cloned.html());

Also note that .html() gets the contents of its operand, so the outer "shell" won't show up.

Upvotes: 4

Related Questions