jQuery event for HTML5 datalist when item is selected or typed input match with item in the list

62,141

Solution 1

On modern browsers, you can use input event, e.g:

$("#name").on('input', function () {
    var val = this.value;
    if($('#allNames option').filter(function(){
        return this.value.toUpperCase() === val.toUpperCase();        
    }).length) {
        //send ajax request
        alert(this.value);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<input id="name" list="allNames" />
<datalist id="allNames">
    <option value="Adnan1" />
    <option value="Faizan2" />
</datalist>

PS: as input event has better support than datalist element, there is indeed no reason to not use it if you are already using datalist element.

Solution 2

You can use input event for achieving such functionality, as below :

$(document).ready(function() {
  $('#name').on('input', function() {
    var userText = $(this).val();

    $("#allNames").find("option").each(function() {
      if ($(this).val() == userText) {
        alert("Make Ajax call here.");
      }
    })
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="name" list="allNames" />
<datalist id="allNames">
  <option value="Adnan1" />
  <option value="Faizan2" />
</datalist>

Solution 3

Simple solution

document.getElementById('name').addEventListener('input', function () {
   console.log('changed'); 
});
Share:
62,141
Mohammad Adnan
Author by

Mohammad Adnan

 Launched brand new AWS ML service ( https://aws.amazon.com/lookout-for-metrics/ )  Software Engineer with about 11+ years of experience in product and applications development.  Subject matter expert for AWS technologies  Recognition for ownerships and result oriented approach.  Working experience in test driven development and agile/scrum/kanban methodologies with continuous integrations and weekly release cycles.  Strong exposure on developing serverless systems  Proficient in grasping new technical concepts quickly and utilizing in effective manner.  Specialized in launching new services with keeping very high standard on operational sustainability.

Updated on April 15, 2020

Comments

  • Mohammad Adnan
    Mohammad Adnan about 4 years

    I have datalist like below -

    <input id="name" list="allNames" />
    <datalist id="allNames">
        <option value="Adnan1"/>  
        <option value="Faizan2"/>   
    </datalist>
    

    What I want is, when an item is typed in completely(for example say in input box when user completely type "Adnan1") or selected from list, then I want an event. I tried couple of approaches but both doesn't help me so far. Approaches are -

    $("#name").change(function(){
    console.log("change");
    }
    

    problem with this is, the event only gets triggered when input gets out of focus I.e. when I click somewhere in the screen.

    I also tried

    $("#name").bind('change', function () {
        console.log('changed'); 
    });
    

    but the callback gets triggered each time when I type in. I actually need to make an AJAX call when item is completely selected. Either via type-in or by selecting from dropdown.

    First approach is bad for user perspective because he has to make extra click and second has disadvantage as for every letter an event will be triggered.

    All I want is an event when either user made a selection or typed complete sentence. is there a way to achieve this? any event that I missing and that can solve my problem.