I've been working on a program that retrieves data from a database, lists some of the data when a button is clicked within the same div, and then creates new divs with buttons that have onclick events until it reaches a certain maximum value. To keep this example more generalizable for others, I'm using placeholder numbers instead of actual database values (for instance, assigning the value 5 to a variable called 'levmax'). The main issue I'm encountering is aligning all the created divs next to each other. Most alignment examples I've found focus on HTML-created divs rather than those generated through JavaScript.
The code is functioning as intended, except for the part where the divs need to be laid out horizontally. The code includes around 80 lines, but my primary concern is achieving proper div alignment. Here's the existing script:
<html>
<head>
<script>
var lev = 0;
var levmax=5;
var count = 0;
function addBu(){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<3; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
if(lev===1){
bu.addEventListener("click", novo);
}
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
};
function novo(){
if(lev===1){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<4; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.addEventListener("click", repeat);
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
}
function repeat(){
if(lev<levmax){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<2; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.addEventListener("click", repeat);
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
var le=(lev-1).toString();
document.getElementById(le).removeEventListener("click", repeat);
}
}
}
</script>
</head>
<body id="kod" onload="addBu()">
</body>
</html>enter code here
<head>
<script>
var lev = 0;
var levmax=5;
var count = 0;
function addBu(){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<3; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
if(lev===1){
bu.addEventListener("click", novo);
}
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
};
function novo(){
if(lev===1){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<4; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.addEventListener("click", repeat);
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
}
function repeat(){
if(lev<levmax){
lev++;
var bo = document.getElementById("kod");
var di = document.createElement('DIV');
for (var i=0; i<2; i++){
var bu= document.createElement('BUTTON');
var te = document.createTextNode('Text');
var te1 = document.createTextNode(lev.toString());
var br = document.createElement('BR');
bu.addEventListener("click", repeat);
bu.id=lev.toString();
bu.appendChild(te);
bu.appendChild(te1);
di.appendChild(bu);
di.appendChild(br);
count++;
}
bo.insertBefore(di, bo.childNodes[0]);
var le=(lev-1).toString();
document.getElementById(le).removeEventListener("click", repeat);
}
}
}
</script>
</head>
<body id="kod" onload="addBu()">
</body>
</html>