Skip to content

Instantly share code, notes, and snippets.

@bachue
Created October 9, 2013 05:34
Show Gist options
  • Save bachue/6896673 to your computer and use it in GitHub Desktop.
Save bachue/6896673 to your computer and use it in GitHub Desktop.
an example about dynamic datalist, work for chrome & firefox
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails: Welcome aboard</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(function() {
$('#input').on('input', function(e) {
var val = $(this).val();
if(val === "") return;
$.get('/query', {text: val}, function(res) {
var datalist = $('#datalist');
datalist.empty();
res = JSON.parse(res);
$.each(res, function (index, ele) {
var opt = $('<option></option>').attr('value', ele);
datalist.append(opt);
});
});
});
});
</script>
</head>
<body>
<div>
<input type="text" id="input" list="datalist"/>
<datalist id="datalist"></datalist>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment