here is my code
const API_KEY = “my_api_key”;
const submit = document.querySelector(“#submit_btn”);
const inputPrompt = document.querySelector(“#real__prompt”);
const imgElement = document.querySelector(“.image_section”)
const getImages = async () => {
const options = {
method: “POST”,
headers: {
“Content-Type”: “application/json”,
“Authorization”: Bearer ${API_KEY}
},
body: JSON.stringify({
“model”: “dall-e-2”,
“prompt”: “full view and white walls in the background in a cozy room where a” + inputPrompt.value,
“n”: 1,
“size”: “256x256”
})
}
try {
const response = await fetch(‘https://api.openai.com/v1/images/generations’, options)
const data = await response.json();
console.log(data);
$(“.loader”).removeClass(“show”)
data?.data.forEach(imageObject => {
const ImageContainer = document.createElement(“div”);
const anchorContainer = document.createElement(“button”);
ImageContainer.classList.add(“Image__container”);
anchorContainer.setAttribute(“id”, “Download_btn”);
const imageElement = document.createElement(“img”);
imageElement.setAttribute(“src”, imageObject.url);
anchorContainer.append(“Download”);
ImageContainer.append(imageElement);
ImageContainer.append(anchorContainer);
imgElement.append(ImageContainer);
const btn = document.getElementById(‘Download_btn’);
const url = imageObject.url;
btn.addEventListener('click', (event) => {
event.preventDefault();
console.log('ABC')
downloadImage(imageObject.url);
})
function downloadImage(url) {
const link = document.createElement("a");
link.href = url;
link.download = document.getElementById("Download_btn").value.split(" ").join("_") + ".png";
link.target = "_blank";
link.click();
}
})
} catch (error) {
console.error(error)