DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Advanced Cloning in UI Development

Advanced Cloning in UI Development

We generally use HTML element cloning in some web applications, but the default jQuery cloning method does not remove data in the newly cloned element. Hence, we came up with this solution called Advanced Cloning—the cloning of HTML elements without duplication of data.

Vmoksha Technologies user avatar by
Vmoksha Technologies
·
Apr. 18, 16 · Web Dev Zone · Analysis
Like (1)
Save
Tweet
3.86K Views

Join the DZone community and get the full member experience.

Join For Free

User Interface (UI) development is one of the most important activities that influences the visual appearance of a web application and is usually associated with certain hurdles while adding content on a page.

Building the most appropriate UI with suitable characteristics such as fast loading, minimum space utilization, minimized scrolling, responsiveness, etc. is imperative for any organization. We usually try different solutions to overcome such hurdles to finally fulfil the requirement.

While working on one such project, I found it necessary to create dynamic form fields. 

What Does Advanced Cloning Mean?

We generally use HTML element cloning in some web applications, but the default jQuery cloning method does not remove data in the newly cloned element. Hence, we came up with this solution called Advanced Cloning. It involves cloning a parent element to a child element and removing data or content from the child element.

In short, Advanced Cloning can also be defined as the cloning of HTML elements without duplication of data. 

Where Can We Use Advanced Cloning?

If we consider a job portal, users need to update their skills while registering their details. For this process, the portal requires an option to add multiple skills in a form. In this scenario, we can implement Advanced Cloning concept and fulfil the requirements of the user. Please refer to the snapshot below to get a better idea of Advanced Cloning usage.

Image title

Implementation of "Advanced Cloning" Using Jquery

Here are the steps to be followed while implementing this concept in HTML pages:

Step 1

Add jQuery library in your code as:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

Step 2

Add the following code into a JS file such as jQuery-advanced-cloning.js:

jQuery(function(){
var addmorehelper = function (match, capture){
return '__' + ((+capture + 1) || '');
};
jQuery(document).on("click",".add-more",function(){
var wrapper_name = '.add-more-wrapper';
var wrapper = jQuery(this).closest(wrapper_name);
var cur_num = wrapper.children('.clone-content').size();
var clone = wrapper.clone().children('.clone-content:last')
.find('input[type="text"],input[type="file"]').val('').end()
.find('textarea').val('').end()
.find('.remove_unwanted').remove().end()
.find('.add-more').val('Remove').removeClass('plus_icon add-more')
.addClass('remove_icon remove-more').end()
.find('select option[value=""]').attr("selected",true).end()
.find("*").each(function(index, element) {
if(element.id){
var matches = element.id.match(/(.+)__\d+/);
if(matches && matches.length >= 1){
element.id = element.id.replace(/__(\d+)?/, addmorehelper)
}
if(element.name){
var matches = element.name.match(/\[([^}]+)\]/);
if(matches && matches.length >= 1){
var st =element.name;
element.name = [].map.call(st,
function(s,i){
return (!isNaN(+s)&&st[i-1]==='['&&st[i+1]===']')?++s:s;
}).join('');
}
}
}
}
).end();
wrapper.append(clone);
});
/*Remove more button */
jQuery(document).on("click",".remove-more",function(){
var wrapper = jQuery(this).closest('.clone-content').remove();
});
});

Step 3

If you need to upload multiple images, you can use the HTML code as shown below:

<ul>
<li>Add Images</li>
<li>
<div class = "row">
<div class = "col-md-6 pad-10">
<input type="file" name="product_image[]" />
<input type="button" name="add_field" value="Add More" class="add-more”/>
</div>
</div>
</li>
</ul>

This code basically has three important elements with a specific class:

Wrapper Element that contains "add-more-wrapper" class. It is an HTML element to append all clone elements.

Clone Content Element that contains "clone-content" class. It contains content and HTML that need to be cloned.

Add More Button Element that contains "add-more" class. It is the element that calls a JavaScript code and does the magic when the user clicks on the button.

For more information pls visit : http://vmokshagroup.com/

Cloning Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Optimize MySQL Queries for Speed and Performance
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0
  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Kafka Fail-Over Using Quarkus Reactive Messaging

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo