Search code examples
jqueryremoveclass

Removing classes from a .html() set variable. Jquery


<div>
    <input type="text" placeholder="#" class="error">
    <input type="text" placeholder="#" class="error">
</div>  

var copylast = $('div').html();

I'm trying to copy the DIV but remove the class error from the copylast variable (I need to keep the classes within the original div.)

I've tried a string replace var copylast = copylast.replace('class="error"', ''); but it only removes the first instance, plus looks tacky. I'd much rather use removeClass('error') if possible.


Solution

  • You could use .clone() if you don't want to manipulate the DOM.

    var copylast = $('div').clone().find('input').removeClass('error').end().html();