Skip to content

Instantly share code, notes, and snippets.

@LucianoCharlesdeSouza
Last active January 11, 2021 20:24
Show Gist options
  • Save LucianoCharlesdeSouza/379c55c892308f55bb6dc9854b2761d2 to your computer and use it in GitHub Desktop.
Save LucianoCharlesdeSouza/379c55c892308f55bb6dc9854b2761d2 to your computer and use it in GitHub Desktop.
<input type="text" list="mysearch" name="search" placeholder="pesquise aqui...">
<datalist id="mysearch">
<option value="Luciano" data-id="1">
<option value="Fernando" data-id="2">
<option value="Paulo" data-id="3">
</datalist>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script>
$(function(){
$('input[name=search]').change(function(){
var inputValue = $(this).val();
var optionSelected = $(`#mysearch option[value=${inputValue}]`)[0]
if(optionSelected){
console.log(optionSelected.dataset.id)
}
})
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment