Untitled
unknown
plain_text
4 years ago
1.3 kB
14
Indexable
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
*{
margin:0;
padding:0;
}
#container{
width:100vw;
height:100vh;
background-color: bisque;
display: flex;
justify-content: start;
gap:5px;
flex-wrap: wrap;
}
.box{
height:100px;
width:100px;
background-color: blue;
font-size: 2rem;
font-weight: bold;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<body>
<button onclick="tambahKotak()">TAMBAH KOTAK</button>
<div id="container">
</div>
</body>
<script>
let container = document.querySelector("#container");
//on the fly, nambahno element secara runtime
let arr = [];
// arr.splice -> buat ngilangin array index ke ... sebanyak ...
function tambahKotak(){
let kotak = document.createElement("div");
kotak.className = "box";
kotak.innerText = arr.length+1;
arr.push(kotak);
container.append(kotak);
}
</script>
</html>Editor is loading...