locked
What is the most effecient way using Javascript to add a div at the end of an existing div, not after it, but in the actul div? RRS feed

  • Question

  • User-858993849 posted

    If I have the following:

    <div class="test1" onclick="addDiv();">
    <content class=contentClass> <span >Test 2</span>
    </content> <!--<div class="Test3"></div>--> </div>

    Using JavaScript, I want to be able to click on the main div (addDiv();), and the have the div that is commented out programmably added.? I assume I have to use an appendto, but how do I do it?

    Thursday, April 26, 2018 1:36 AM

Answers

  • User-369506445 posted

    hi

    you can use append syntax in java script for add to end of element

    $( ".your class" ).append( "<p>Test</p>" );

    your complete sample :

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Example of CSS Hacks for CHROME and INTERNET EXPLORER</title>
    <link rel="stylesheet" type="text/css" href="hacks.css">
        <script src="Scripts/jquery-1.10.2.min.js"></script>
        <script>
            function addDiv() {
                $(".test1").append("<div class='Test3'>test2</div>");
            }
        </script>
    </head>
    
    <body>
    <div class="test1" onclick="addDiv();">
         <content class=contentClass>
             <span >Test 2</span>
         </content>
        <!--<div class="Test3"></div>-->
    </div>
    </body>
    
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 26, 2018 7:49 AM