Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically updated datalist won't show

I'm updating an html5 datalist dynamically, as the user types, with the following script:

$('#place').on('keyup', function() {
    $.post('content/php/autocomp.php', { field: 'plaats', val: $('#place').val() }).done(function(response) {
        $('#autocomp-places').html(response);
    });
});

Which works fine except that the datalist often doesn't show right away. When I inspect the element the html is there but the datalist is not shown as soon as it's updated. How can I force it to show?

For the record: it works... I just wish it would always show the new suggestion right away.

like image 215
Galadre Avatar asked May 23 '13 13:05

Galadre


People also ask

Is Datalist new to HTML5?

The HTML <datalist> tag is is used to provide an auto complete feature on form element. It provides a list of predefined options to the users to select data. The datalist tag is introduced in HTML5. The <datalist> tag should be used with an <input< element that contains a "list" attribute.

Is Datalist supported by all browsers?

You can see that each browser displays a tick mark for each datalist option provided. Additionally, Chrome will snap the slider to these predefined values as the user moves the slider near them. Unfortunately, Internet Explorer and Chrome are the only browsers to support datalists on range inputs at this time.

What does Datalist mean in HTML?

Definition and Usage The <datalist> tag specifies a list of pre-defined options for an <input> element. The <datalist> tag is used to provide an "autocomplete" feature for <input> elements. Users will see a drop-down list of pre-defined options as they input data.


2 Answers

Please use success instead of done method of ajax and try again.

$('#place').on('keyup', function () {
    $.post('content/php/autocomp.php', {
        field: 'plaats',
        val: $('#place').val()
    }).success(function (response) {
        $('#autocomp-places').html(response);
    });
});
like image 102
Harsh Sanghani Avatar answered Nov 12 '22 05:11

Harsh Sanghani


I think I just have found a decent workaround for this!

Here is my pseudo-code:

  1. As I type, I make async httprequests to get data.
  2. When data is returned, i clear and re-populate the datalist.
  3. If the current input field is still focused, manually call .focus() on the input element (this seems to force the data-list popup behavior to occur).
like image 39
AlvinfromDiaspar Avatar answered Nov 12 '22 04:11

AlvinfromDiaspar