`;
document.body.insertAdjacentHTML('beforeend', modalHtml);
// Make the element draggable
function dragElement(element, dragHandle) {
var pos1 = 0,
pos2 = 0,
pos3 = 0,
pos4 = 0;
// if present, the header is where you move the DIV from:
if (dragHandle) {
// if header is specified, only start the drag process when the header is clicked
dragHandle.onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV
element.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
element.style.top = (element.offsetTop - pos2) + "px";
element.style.left = (element.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
// Apply the drag function to your element
dragElement(document.getElementById("exchangeModal"), document.getElementById("modalHeader"));
dragElement(document.getElementById("showModalButton"), document.getElementById("showModalButton"));
// Function to show the modal
function showModal() {
fetchExchangeRate();
document.getElementById('exchangeModal').style.display = 'block';
document.getElementById('showModalButton').style.display = 'none';
}
// Function to hide the modal and show the small box
function hideModal() {
document.getElementById('exchangeModal').style.display = 'none';
document.getElementById('showModalButton').style.display = 'block';
}
// Function to fetch the exchange rate
function fetchExchangeRate() {
// The URL of the API providing exchange rate data.
// Make sure to replace `YOUR-API-KEY` with your actual API key if required.
const apiURL = 'https://api.exchangerate-api.com/v4/latest/USD';
fetch(apiURL)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
// Assuming the API returns a JSON object with a property `rates` containing the rates.
const rate = data.rates.PEN;
// Set the exchange rate in your modal.
document.getElementById('usdValue').value = rate.toFixed(2);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
fetchExchangeRate();
// Event listener for close button
document.getElementById('closeButton').addEventListener('click', hideModal);
// Event listener for the small box to show the modal
document.getElementById('showModalButton').addEventListener('click', showModal);
// Event listeners for other buttons as needed
// ...
})();