I have an div in my HTML body titled recipeContainer. I am trying to use an API to search for recipes based on keywords from the user. Originally, I had 6 divs in an HTML that were populated in my javascript and styled in CSS, but that is no longer an efficient solution I think.
Here is my current code in the attempt to transfer functionality into each recipe's tile being created entirely in JS :
for(i = 0; i < 5 ; i++){
let div = document.createElement("div"+i);
div.style.color = "gray";
div.style.width = "200px";
div.style.height = "350px";
div.style.display = "flex";
div.style.textAlign = "center";
div.style.borderRadius = "15px";
div.style.flexDirection = "column";
document.getElementById("recipeContainer").appendChild(div);
}
This is the loop executed whenever a user hits submit. I actually have two questions pertaining to this, firstly, I expected this code to create 5 200px x 350px gray tiles in my recipeContainer div, but nothing appears. What am I missing?
Secondly, when it comes to appending info from my API call(such as recipe name, food image, etc.), how would I go about appending to my selected div? Could I just declare a variable(ex. foodImg) getting that information from my JSON data, and do div.appendChild(foodImg)
, as well as contain all the styling of the element I'm trying to add, inside of the javascript?
${Title}\``, using a combo of innerHTML and template strings to populate your contents.
– somethinghere Mar 25 '21 at 13:49