简体   繁体   中英

how do i create a new DIV each time a button is clicked?

i'm trying to create a DIV with 100x100 px and it shold be red, every time a button is clicked, i did this code but its not working:

    <html>
<title>
    create new div
</title>

<body>
<button id="createsquare">
Cria QUADRADO
</button>

<script>
function fcreatesquare()
{
var crsq = document.createElement('div', 'id=created', 'widght=100px', 'wheidght=100px','color=black');
return crsq;
}

    var createsquare = document.getElementById('createsquare');
    createsquare.onclick(fcreatesquare);
</script>
</body>

</html>

Check out the appendChild function: https://www.w3schools.com/jsref/met_node_appendchild.asp

function fcreatesquare() {
var crsq = document.createElement('div', 'id=created', 'widght=100px', 'wheidght=100px','color=black');
document.getElementsByTagName("body")[0].appendChild(crsq);
}

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