<div id="autocomplete-container">
<input type="text" autofocus="true" name="autofocus sample" placeholder="Search by Country" id="autocomplete-input"></input>
<ul id="autocomplete-results">
</ul>
</div>
/*CSS RESET*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font: inherit;
font-size: 100%;
vertical-align: baseline;
}
html {
line-height: 1;
}
ol, ul {
list-style: none;
}
/*STYLES*/
body {
background: #123;
height: 100%;
width: 100%;
font-family:"Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif;
}
#autocomplete-container {
width: 300px;
margin: 50px auto;
}
#autocomplete-input {
background: white;
appearance: none;
appearance: none;
width: 100%;
padding: 20px 15px;
border: pink;
outline: none;
color: black;
font-size: 20px;
}
#autocomplete-input:placeholder {
color: grey;
}
#autocomplete-input::placeholder {
color: grey;
}
#autocomplete-results {
display: none;
width: 100%;
margin-top: -2px;
color: #eee;
}
#autocomplete-results li {
width: 100%;
padding: 7px 15px;
border-right: 2px solid #eee;
border-bottom: 2px solid #eee;
border-left: 2px solid #eee;
}
#autocomplete-results li:hover {
background:rgba(105, 111, 118, 0.8);;
}
function Get(url) {
var HttpReq = new XMLHttpRequest();
HttpReq.open("GET", url, false);
HttpReq.send(null);
return HttpReq.responseText;
}
// variables
var input = document.querySelector("input");
var results, countries_to_show = [];
var autocomplete_results = document.getElementById("autocomplete-results");
// functions
function autocomplete(val) {
var countries_returned = [];
var countries = JSON.parse(
Get("https://restcountries.eu/rest/v2/name/" + val + "?fields=name")
);
console.log(countries);
countries.map(function(element, index) {
countries[index] = element.name;
});
for (i = 0; i < countries.length; i++) {
if (val === countries[i].toLowerCase().slice(0, val.length)) {
countries_returned.push(countries[i]);
}
}
return countries_returned;
}
// events
input.onkeyup = function(e) {
input_val = this.value.toLowerCase();
if (input_val.length > 0) {
autocomplete_results.innerHTML = "";
countries_to_show = autocomplete(input_val);
for (i = 0; i < countries_to_show.length; i++) {
autocomplete_results.innerHTML +=
"<li id=" +
countries_to_show[i] +
' class="list-item">' +
countries_to_show[i] +
"</li>";
}
autocomplete_results.style.display = "block";
} else {
countries_to_show = [];
autocomplete_results.innerHTML = "";
}
};
document
.getElementById("autocomplete-results")
.addEventListener("click", function(e) {
if (e.target && e.target.nodeName == "LI") {
console.log(e.target.innerHTML);
input.value = e.target.innerHTML;
autocomplete_results.innerHTML = null;
}
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |