So I do have a list containing 8 to 10 elements maximum and I want to add a button in a certain ejs page depending on how many items do I have on the list. for example : ["Salad","Tomato","Paste"] then 3 buttons will be added on the page and so on...
app.post('wanttogo', function (req, res) {
});
the above is the post function in which the implementation is needed is there such thing as I described in javascript to dynamically add a button depending on loop or something. Thanks in advance,
I have little knowledge on javascript and I tried to google what I want but the results where amibiguous.
CodePudding user response:
let buttons_from_post = ["Salad","Tomato","Paste"];
buttons_from_post.forEach(addBtn);
function addBtn(value, index, array) {
let btn = document.createElement("button");
btn.innerHTML = value;
btn.onclick = function () {
alert("Button is clicked");
};
document.body.appendChild(btn);
}
here is an example of doing so