All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Append Multiple Elements

Last Updated : Mar 11, 2024

jQuery Append Multiple Elements

In this article we will show you the solution of jQuery append multiple elements, here we needs to use click() and append() methods.

The append() method inserts specified content at the end of the selected elements and click() method as we know used for loads some process which is appended with html elements which triggers click event when user clicks on it.

Step By Step Guide On jQuery Append Multiple Elements :-

Here we defined html block with ul element with id ‘parentList’ and listed one item ‘Frozen’, button ‘Add Elements’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that we waiting for click event occurrence which is appended with ‘button’ there we created array ("Luck", "Encanto", "Tangled") and stored on variable ‘arr’.

Then we defined for loop for appends array elements until it meets array length by using append method appended li elements added on ‘parentList’ ul element with array values.

<!DOCTYPE html>
<html>
    <head>
        <title>Append Multiple Elements</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                $('button').click(function(){
                    var arr=[
                        "Luck", "Encanto", "Tangled"
                    ];
                    for(i=0;i<arr.length;i++){
                        $('#parentList').append('<li>'+arr[i]+'</li>');
                    }
                });
                });
        </script>
        </head>
        <body>
            <ul id="parentList">
                <li>Frozen</li>
            </ul>
            <button>Add Elements</button>
        </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we appending click() method with button by using ‘button’ tag name for generate click event when user clicks on ‘Add Elements’ button.
  8. Within function we created array variable ‘arr’ with values ("Luck", "Encanto", "Tangled") and defined for loop with condition until iteration count reach length of ‘arr’, there we appending append() method with div element id ‘parentDiv’.
  9. Within append() method created li element by ‘$("<li></li>")’ and which is created with array values by ‘arr[i]’. So it will adds li element on ul element with the name of array values.
  10. In html block we defined ul element with id ‘parentList’ which contains list ‘Frozen’ element and button ‘Add Elements’.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to append multiple elements using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can see listed element ‘Frozen’ and button ‘Add Elements’ now user needs to click on ‘Add Elements’ button then we can see appended multiple list elements ‘Luck, Encanto, Tangled’ on webpage.

I hope this article on jQuery append multiple elements helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪