Avatar of nb2009
nb2009

asked on 

Clone an element, remove it and place it in another place

Dear Experts,

I have a page where I need to move a <TR> element from one place to another.
For this, I'm using jquery to create a cloned object then remove the original element, and place the cloned object in it's new place.

However, this doesn't work.
It will remove my element, but won't place the cloned object where I want it to be placed,
have a look at my code below

Thanks in advance!
//get the tbody that we want to manipulate
var treatmentMainBody = $("#npa_treatmentname1").parents('tbody').get(0);
 
for(var i = 1; i <= 10; i++)
{
	/*create one cell for the treatment name & one cell for the treatment
	type. */
	
	var newCell = treatmentMainBody.rows[i + 1].insertCell(2);
	var newTreatmentTypeCell = treatmentMainBody.rows[i + 1].insertCell(3);
	
	//create cloned fields and remove the originals
	var clonedTreatmentNameField = $("#npa_treatmentname" + i).clone();	
	var clonedTreatmentTypeField = $("#npa_treatmenttype" + i).clone();
	
	$("#npa_treatmenttype" + i).remove();
	$("#npa_treatmentname" + i).remove();	
	
	//append the smart field to the tbody
	$(newCell).append(clonedTreatmentNameField);
	$(newTreatmentTypeCell).append(clonedTreatmentTypeField);
}

Open in new window

JavaScriptWeb Applications

Avatar of undefined
Last Comment
nb2009

8/22/2022 - Mon