So I am new to Javascript and I wanted to know how I should append the variable red_btn to my document. The code currently does not work. I am not used to using functions so is my code correct. I wanted to make a few more buttons in Buttons() function. Please help me out.
function CreateCircle(r, color){
let canvas = document.getElementById("MyCanvas");
let ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath()
ctx.arc(240, 200, r, 0 , 2 * Math.PI);
ctx.fillStyle = color;
ctx.stroke();
ctx.fill();
}
function Buttons(){
const red_btn = document.createElement("button");
red_btn.innerHTML = "Button";
red_btn.name = "red_btn";
red_btn.type = "button";
red_btn.innerText = "Red";
}
document.body.appendChild(Buttons.red_btn);
It should be just the following:
function Buttons(){
const red_btn = document.createElement("button");
red_btn.innerHTML = "Button";
red_btn.name = "red_btn";
red_btn.type = "button";
red_btn.innerText = "Red";
return red_btn;
}
document.body.appendChild(Buttons());