Clone div and delete class without changing the original

I want to clone the contents of a div using jQuery, but from the copied content I want to remove the class from the source before using the appendTo function. When I delete a class from a clone, they are also deleted from the original.

My code is:

$('.carousel .item').each(function(){ var next = $(this).next(); if (!next.length) { next = $(this).siblings(':first'); } next.children(':first-child').clone().appendTo($(this)); next.children(':first-child').addClass('col-sm-offset-1'); for (var i=0;i<3;i++) { next=next.next(); if (!next.length) { next = $(this).siblings(':first'); } next.children(':first-child').clone().appendTo($(this)); } }); 

Please note: I do not want to remove the class from the actual div where I copied the content, I just want to remove it from the copied code so that it is not included in the cloned div.

+5
source share
3 answers

You can use removeClass and addClass after clone like this.

  .clone().removeClass('oldClass').addClass('col-sm-offset-1') 
+3
source

First, you can remove an element from the cloned object and then clone it to a new object, which will look like this:

 $('.carousel .item').each(function(){ var next = $(this).next(); if (!next.length) { next = $(this).siblings(':first'); } var $block = next.children(':first-child'); // Grab the and remove element class $block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class'); // Clone the block var $clone = $block.clone(); $clone.appendTo($(this)); next.children(':first-child').addClass('col-sm-offset-1'); for (var i=0;i<3;i++) { next=next.next(); if (!next.length) { next = $(this).siblings(':first'); } var $block = next.children(':first-child'); // Grab the and remove element class $block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class'); // Clone the block var $clone = $block.clone(); $clone.appendTo($(this)); } }); 

Replace "your-element-class-id" with your class name that you want to remove. Original link. from - How to remove the selected item during the clone () operation

0
source

You should be able to run removeClass () on an object before adding it, no matter where you want to add it.

0
source

Source: https://habr.com/ru/post/1268204/


All Articles