Reputation: 5241
I am using the following script to copy classes from one element to another:
jQuery("#copyto").attr("class", jQuery("#copyfrom").attr("class"));
#copyfrom
looks like this:
<div class="blah active">
I'd like to exclude the 'blah' class and only copy 'active'.
Is that possible some how?
Upvotes: 2
Views: 1333
Reputation: 303261
Option 1 (old school):
var copyfrom = document.getElementById('copyfrom'),
copyto = document.getElementById('copyto');
var old = " "+copyfrom.className+" ";
copyto.className = old.replace(' blah ','');
// alternatively
copyto.className = copyfrom.className.replace(/(^|\s)blah(\s|$)/,'');
Option 2 (new school):
copyto.className = copyfrom.className;
copyto.classList.remove( 'blah' );
Option 3 (just jQuery):
$('#copyto').attr('class',$('#copyfrom').attr('class')).removeClass('blah');
Upvotes: 2