简体   繁体   中英

Add dynamic elements on button click

When I click on button Add, I want a dynamic button to be added into the div. I tried but couldn't do it. And I cannot figure out what is where the mistake is, in the code. Here's my code.

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<html>
    <head>
        <title>Content</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!-- Bootstrap -->
        <link href="stylesheets/bootstrap.min.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap-theme.min.css" rel="stylesheet" type="text/css">
        <link href="stylesheets/bootstrap-theme.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
            function addinfo()
            {
                //Create an input type dynamically.   
                element = document.createElement("button");
                element.className='btn btn-default';
                var t=document.createTextNode("Edit");
                element.appendChild(t);
                element.id=t;
                //var account=document.getElementById('newaccname').value;

                var foo = document.getElementById("acc");
//                //Append the element in page (in span).  
                foo.appendChild(element);
//                    var d = document.getElementById('acc');
//                    d.appendChild(i);
            }
        </script>
    </head>
    <body>
        <button type="submit" name="addaccount" class="btn btn-default" onlick="addinfo()">Add</button>
<div id="acc" style="width:500px;height:100px;border:1px solid #000;"></div>
</body>
</html>

You have a typo in your code. onlick -> onclick

onlick="addinfo()"

should obviously be

onclick="addInfo()"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM