Last active
May 5, 2017 09:19
-
-
Save oskarrough/92c312cc17f50974f09294b16c4000d7 to your computer and use it in GitHub Desktop.
Native autocomplete for Radio4000 channels (source https://jsbin.com/daruqo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="native autocomplete with ajax"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Radio4000 autocomplete</title> | |
<style id="jsbin-css"> | |
input { | |
padding: 0.5em; | |
} | |
</style> | |
</head> | |
<body> | |
<p>What is the name of your Radio4000?</p> | |
<input id="radio4000-autocomplete" list="radio4000-channels"> | |
<datalist id="radio4000-channels"></datalist> | |
<script id="jsbin-javascript"> | |
// Get the <datalist> and <input> elements. | |
var dataList = document.getElementById('radio4000-channels') | |
var input = document.getElementById('radio4000-autocomplete') | |
// Update the placeholder text. | |
input.placeholder = "Loading radios..." | |
function afterModel(channels) { | |
channels.forEach(function(channel) { | |
var option = document.createElement('option') | |
option.value = channel.title | |
option['data-slug'] = channel.slug | |
dataList.appendChild(option) | |
}); | |
// Update the placeholder text. | |
input.placeholder = 'e.g. "Radio Superman"' | |
} | |
fetch('https://api.radio4000.com/v1/channels') | |
.then(res => res.json()) | |
.then(data => afterModel(data)) | |
.catch(err => { | |
console.log(err) | |
}) | |
</script> | |
<script id="jsbin-source-css" type="text/css">input { | |
padding: 0.5em; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Get the <datalist> and <input> elements. | |
var dataList = document.getElementById('radio4000-channels') | |
var input = document.getElementById('radio4000-autocomplete') | |
// Update the placeholder text. | |
input.placeholder = "Loading radios..." | |
function afterModel(channels) { | |
channels.forEach(function(channel) { | |
var option = document.createElement('option') | |
option.value = channel.title | |
option['data-slug'] = channel.slug | |
dataList.appendChild(option) | |
}); | |
// Update the placeholder text. | |
input.placeholder = 'e.g. "Radio Superman"' | |
} | |
fetch('https://api.radio4000.com/v1/channels') | |
.then(res => res.json()) | |
.then(data => afterModel(data)) | |
.catch(err => { | |
console.log(err) | |
})</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input { | |
padding: 0.5em; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Get the <datalist> and <input> elements. | |
var dataList = document.getElementById('radio4000-channels') | |
var input = document.getElementById('radio4000-autocomplete') | |
// Update the placeholder text. | |
input.placeholder = "Loading radios..." | |
function afterModel(channels) { | |
channels.forEach(function(channel) { | |
var option = document.createElement('option') | |
option.value = channel.title | |
option['data-slug'] = channel.slug | |
dataList.appendChild(option) | |
}); | |
// Update the placeholder text. | |
input.placeholder = 'e.g. "Radio Superman"' | |
} | |
fetch('https://api.radio4000.com/v1/channels') | |
.then(res => res.json()) | |
.then(data => afterModel(data)) | |
.catch(err => { | |
console.log(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment