All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Create Multiple DIV Dynamically In jQuery

Last Updated : Mar 11, 2024

How To Create Multiple DIV Dynamically In jQuery

In this article we will show you the solution of how to create multiple div dynamically in jQuery, here we needs to use val(), append() and prop() methods.

As we know prop() method used for sets any properties value on any html elements so we can add some text and class on div element and append() method used for creates div element which is added on html page then using val() method we collecting div elements count from user in dynamically.

Step By Step Guide On How To Create Multiple DIV Dynamically In jQuery :-

Here we defined html block with input element for collects div elements count dynamically, button for loads these process when it’s get triggered and div element ‘parentDiv’ for appending created div elements on webpage.

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 collecting input elements value and stored on variable ‘cnt’.

Then we defined for loop for generate div elements based on dynamic user input using append method created div element added on ‘parentDiv’ div element with some message and class name ‘bdr’.

<!DOCTYPE html>
        <title>Multiple Div Dynamically</title>
        <script src=""></script>
            $(document).ready(function (){
                            var cnt=$('#inp').val();
                                innerHTML:'This is div element'+i,
                    width: fit-content;
                    padding: 10px;
                    border: 2px solid blue;
                    margin: 10px;
            <input type="text" id="inp" placeholder="Enter Div Counts">
            <button>Add Divs</button>
            <div id="parentDiv"></div>
  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 Divs’ button.
  8. Within function we collecting dynamic input from user for generates div element based on it value and stored on variable ‘cnt’ by using val() method when added with input element ‘#inp’.
  9. We defined for loop with condition until iteration count reach value to ‘cnt’, there we appending append() method with div element id ‘parentDiv’.
  10. Within append() method created div element by ‘$("<div>")’ and which is created with text by specifying ‘innerHTML’ property value to ‘'This is div element'+i’, sets class attribute’s value as ‘bdr’ by using prop() method.
  11. In style block we defined class ‘bdr’ with some properties of ‘width set to content width, padding sets to 10px, border width set to ‘2px’, border color sets to blue and at last margin set to 10px’.
  12. In html block we defined we defined input element with text type, id ‘inp’, placeholder value set to ‘Enter Div Counts’, button ‘Add Divs’ and div element created with id ‘parentDiv’.
  13. 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 create div elements dynamically 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 input field and ‘Add Divs’ button now user needs to fills the input field and click on ‘Add Divs’ button then we can see multiple div elements with blue border on webpage based on user input count.

I hope this article on how to create multiple div dynamically in jQuery helps you and the steps and method mentioned above are easy to follow and implement.