The reason for the behaviour is that the buttons are displayed inline in the HTML document. If you want to move the buttons freely without interfering with other elements, I suggest you using positition
absolute
and top
/ left
.
Bear in mind that the two last buttons are placed on top of each other since they have identical coordinates.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gavito Auto</title>
</head>
<body>
<div id="button1">
</div>
<script>
//Buttons
//// BUY CAR PARTS BUTTON
function screen_main_button(name, func, moveX, moveY){
// 1. Create the button
var button = {};
button.name = name;
button.func = func;
button.button = document.createElement("button");
button.button.innerHTML = button.name;
button.button.style.position = "absolute";
button.button.style.width = "200px";
button.button.style.height = "200px";
button.button.style.left = moveX;
button.button.style.top = moveY;
button.button.style.backgroundColor = "orange";
// 2. Append to body
var body1 = document.getElementsByTagName("body")[0];
body1.appendChild(button.button);
// 3. Add event handler
button.button.addEventListener ("click", function() {
button.func()
});
}
//BUTTON FUNCTIONS
works = function(){
alert("this is working")
};
//button calls
screen_main_button("Buy Car Parts", works, "370px", "100px");
screen_main_button("Buy A Car", works, "10px", "100px");
screen_main_button("Request A Car Repair", works, "10px", "100px");
</script>
</body>
</html>