#HTML button that fetches JSON link
15 messages · Page 1 of 1 (latest)
Could you share the code ?
Im on a night walk right now haha
Just a screenshot !!
Is enough
Yeah you can do this using ajax it's very simple
Use the xhr method
Imma send you the code ok
Yeah it's possible
Give me the url please
Try this !!
document.getElementById('fetchDataBtn').addEventListener('click', async () => {
const url = 'https://htsit.se/gn/test/test-1.json';
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Failed to fetch data');
}
const jsonData = await response.json();
displayJsonData(jsonData);
} catch (error) {
console.error('Error fetching data:', error);
}
});
function displayJsonData(data) {
const jsonDataDiv = document.getElementById('json-data');
const formattedData = JSON.stringify(data, null, 2);
jsonDataDiv.innerHTML = <strong>Fetch API Response:</strong><pre>${formattedData}</pre>;
}
document.getElementById('changeBinIdBtn').addEventListener('click', () => {
const newBinId = document.getElementById('binIdInput').value.trim();
if (newBinId) {
changeBinId(newBinId);
}
});
Ight , bet
With the add.event.listener
It add the event of click to the selected button