const searchBar = document.getElementById('searchBar'); const resultsContainer = document.getElementById('searchResults'); let allresults = []; window.onbeforeunload=searchBar.value=""; const url = 'https://setaapi.serverissimo.freeddns.org/stoplist'; fetch(url) .then(response => { if (!response.ok) throw new Error("Errore nel caricamento dei dati."); return response.json(); }) .then(data => { allresults = data; }) .catch(error => console.error('Errore nel caricamento dei dati:', error)); searchBar.addEventListener('input', () => { const searchTerm = searchBar.value.toLowerCase(); const filtered = allresults.filter(item => item.fermata.toLowerCase().includes(searchTerm) ); renderresults(filtered); }); function renderresults(results) { const searchResultsContainer = document.getElementById('searchResults'); searchResultsContainer.innerHTML = ''; if (results.length === 0) { searchResultsContainer.innerHTML = '

Nessun risultato trovato

'; return; } results.forEach(item => { const div = document.createElement('div'); div.className = 'search-result'; div.innerHTML = `

${item.fermata}

Codice fermata: ${item.valore}

`; div.addEventListener('click', () => { const url = `fermata.html?code=${encodeURIComponent(item.valore)}&name=${encodeURIComponent(item.fermata)}`; window.open(url, '_blank'); }); searchResultsContainer.appendChild(div); }); }