Clone List Elements using JavaScript

Clone List Element using JavaScript
Code Snippet:List Cloning Vanilla Javascript
Author: Juliana++
Published: January 14, 2024
Last Updated: January 22, 2024
Downloads: 399
License: MIT
Edit Code online: View on CodePen
Read More

This simple JavaScript code snippet helps you to clone list elements and append them to a new ordered list. The core function, “insert,” clones elements with the class “especial” and adds them to the container. This code is helpful for replicating and displaying selected list items in a new context.

You can use this code in your JavaScript programs to dynamically clone and manipulate HTML elements. This allows you to create interactive features like adding user-generated content or dynamic lists in your web apps.

How to Clone List Elements Using JavaScript

1. Before you dive into the JavaScript code, make sure your HTML structure includes the list items you want to clone. In our example, we have a list with special items that we want to duplicate. You can customize this according to your needs.

<div class="wrap">
        <div class="left">
            <ol class="lista">
                <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
                <li class="especial">Nam at ante vehicula, posuere enim a, ornare massa.</li>
                <li>Maecenas tincidunt mauris non blandit gravida.</li>
                <li class="especial">Phasellus eget turpis id lorem pulvinar finibus nec ac ipsum.</li>
                <li>Donec a odio posuere dui elementum mattis.</li>
                <li class="especial"> Nullam id elit ut quam dignissim posuere.</li>
                <li class="especial">Duis sollicitudin risus ac lectus bibendum, eget cursus ligula dictum.</li>
            </ol>
        </div>
        <div class="container"></div>
    </div>

2. If you want to style the list elements, you can use the following CSS code. (Optional)

*{
    padding: 0;
    margin: 0;
}
.wrap{
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 50vw 50vw;
}
.left{
    height: 100vh;
    width: 50vw;
}
ol{
    display: block;
    padding: 30%;
}

.container{
    background-color:rgb(223, 242, 247);
    padding: 5%;
    height: 100vh;
    width: 50vw;
}

.especial{
    font-family: 'Satisfy', cursive; 
}

3. Now, let’s add the JavaScript code that will clone the list elements. The following code contains the essential functions for cloning and inserting the elements. Be sure to include the code within a <script> tag in your HTML file, or you can link to an external JavaScript file.

let liClassEspecial = document.querySelectorAll("li.especial");
let liArray = Array.from(liClassEspecial);

    
let container = document.querySelector(".container");
let olElement = document.createElement("ol");
container.appendChild(olElement);



var insert = () => {
    for(i=0; i<=liArray.length; i++){
        try {
            var liClone = liArray[i].cloneNode(true);
            olElement.appendChild(liClone);
        } catch(err) {
            err;
        }
    }
}

 insert();

That’s all! hopefully, you have successfully created a functionality to clone list elements. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X