3 minutes reading time (551 words)

Clone html element using jquery


Guys, This blog is about adding multiple html elements using jQuery and display the cloned field. If you are a developer, you might be interested in this. Hope this Helps! :)

So this is Quick guide to create multiple fields as many as with in minimum efforts

Lets' code! Step by step :

A. Add jQuery file from Google API and add required styling-

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
           .qtc_att_hide {display: none;}

B: Lets create template HTML code which you have to create for N times. -

Lets say template html is for text box field and you have to add for N time. Each time while adding the new field via. javascript, we are using this template code to generate the new field. As this is the template will not be shown to user, so we can hide it with the help of class styling qtc_att_hide.

<!--Create one div and hide it. It will act as template for cloning-->
<div id=<?php echo $attribute_container_id ?> class="qtc_container qtc_att_hide" style="" >
	<div class="com_qtc_repeating_block well well-small span10">
		<label class="" ><strong>Add Extra Attribute </strong></label>
                 <!-- Lets take a take a text box field to clone -->
		<input id="atrri_name0" name="atrri_name0" class="input-medium attriFieldClass" type="text" value="" maxlength="250" size="32">
	<!-- required -->


C: Display saved fields and add one more extra fields -

Following code sample displays already saved fields details along with delete button. Also adds one more extra field with blank details so that user can add one more field easily.

// $allAttribues contains previously stored field details
$count = !empty($allAttribues)?count($allAttribues):1;

// For each attribute and one extra field
for ($i = 1;$i <= $count;$i++)
	$attribute_container_id = "qtc_container".$i;
	<div id=<?php echo $attribute_container_id ?> class="qtc_container">

		<div class="com_qtc_repeating_block well well-small span10">
			<label class="" ><strong>Add Extra Attribute </strong></label>
			<input id="atrri_name<?php echo $i; ?>" name="atrri_name<?php echo $i; ?>" class="input-medium attriFieldClass" type="text" value="<?php echo (isset($allAttribues[$i]->itemattribute_name))?$allAttribues[$i]->itemattribute_name:''; ?>" maxlength="250" size="32">

		<!-- Here is remove button for each field -->
		<div class='com_qtc_remove_button pull-left span1'>
			<button class='btn btn-mini' type='button' id='remove<?php echo $i;?>'
				onclick="removeClone('qtc_container<?php echo $i;?>','qtc_container');"                                               title="Remove Field" >
				<i class="icon-minus-2 "></i>
		<!-- required -->

This will look like


D: Add "+" button for add more field functionality

<div class="com_qtc_add_button pull-left  span1">
	<button class="btn btn-mini " type="button" id='add' onclick="addClone('qtc_container','qtc_container');"             title="Add New Field">
		<i class="icon-plus-2 "></i>


E: Finally following JavaScript code will be executed on add more and delete button.

<script type="text/javascript">

	// globle params
	var attribute_current_id = <?php echo $count; ?>;
	F: Finally add more fields using javascript.

	Following function makes a copy of template HTML and appends to the existing fields with the help of function parameters.
	function addClone(rId,rClass)
		// CURRENT ATRIBURE ID -- global declaration
		var num=attribute_current_id;

		// Make a clone from template div
		var newElem=jQuery('#'+rId+'0').clone().attr('id',rId+num);

		// Remove hidden styling

		// Add clone element at the end of other div

		var removeButton="<div class='com_qtc_remove_button pull-left span1'>";
		removeButton+="<button class='btn btn-mini' type='button' id='remove"+num+"'";
		removeButton+="onclick=\"removeClone('"+rId+num+"','"+rClass+"');\"  title=\"Remove Field\">";
		removeButton+="<i class=\"icon-minus-2\"></i></button>";


		var newelementid=	rId+num;
		var option=0;

		var newname='arri_name'+num;
		var ck=newElem.find('.attriFieldClass').attr({'name': newname,'id':newname});

	/** G:This function remove the specified div */
	function removeClone(rId,rClass)

After cloning, fields will look as follow.


Follow the link to download this example CloningExample_20140613-125955_1.zip


5+2 Reasons to Buy JomSocial Essentials Pack
Sorting Nested List using jQuery