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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.