Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!doctype html>
<meta name="description" content="native autocomplete with ajax">
<meta charset="utf-8">
<title>Radio4000 autocomplete</title>
<p>This is an experiment in a single UI-element to select a Radio4000 channel.</p>
<script src="https://cdn.jsdelivr.net/npm/radio4000-sdk@0.0.4/dist/radio4000-sdk.js"></script>
<input id="autocomplete" list="radio4000-channels" placeholder='e.g. "Radio Superman"'>
<datalist id="radio4000-channels"></datalist>
<p>For browser support see <a href="https://caniuse.com/#feat=datalist">https://caniuse.com/#feat=datalist</a></p>
 
var input = document.querySelector('#autocomplete')
var datalist = document.querySelector('#radio4000-channels')
var originalPlaceholder = input.placeholder
input.placeholder = 'Loading radios...'
input.addEventListener('input', function(event) {
    console.log(event.target.value)
})
radio4000Sdk.findChannels().then(channels => {
    insertOptions(channels)
    input.placeholder = originalPlaceholder
})
function createOption(data) {
    var option = document.createElement('option')
    option.label = data.title
    option.value = data.slug
    return option
}
function insertOptions(channels) {
    var sorted = channels.sort((a, b) => a.title.localeCompare(b.title))
    var options = sorted.map(createOption)
    options.forEach(function(option) {
        datalist.appendChild(option)
    })
}
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
oskarpro
0viewers