tags:

views:

29

answers:

1

A simple task of cloning an element and autoincrement its id.

can it be done in 1 line?

+1  A: 

jQuery

var NewElement=$("#test1").clone();  // Clone the element
var IDNr=NewElement.attr("id").match(/\d+$/);  // Grab the ID number

NewElement.attr("id",NewElement.attr("id").replace(IDNr,parseInt(IDNr)+1));  // Increment the ID

HTML

<div id="test1">Test</div>

Code in action.

Gert G