Note that there are some explanatory texts on larger screens.

plurals
  1. POAutocomplete HTML5 input dynamically using datalist & ajax
    primarykey
    data
    text
    <p>I try to update a input in a html form via datalist by updating the datalist itself using jquery ajax calls. in case of using ajax the input does not show the options, <strong>but</strong> when i click into the input field again. the static test version without ajax does work as expected, showing options while typing into the input field.</p> <p>HTML markup</p> <pre><code>&lt;form id="formID" class="form-horizontal"&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="input"&gt;Location&lt;/label&gt; &lt;div class="controls"&gt; &lt;input type="text" list="LIST_CITIES" id="inputCity" placeholder="Enter your location.."&gt; &lt;/div&gt; &lt;/div&gt; &lt;!-- submit --&gt; &lt;button type="submit" class="btn"&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;datalist id="LIST_CITIES"&gt;&lt;/datalist&gt; </code></pre> <p>without ajax (does work):</p> <pre><code>$('.controls').on("input click", "#inputCity", function(e) { var val = $(this).val(); dataList.empty(); if(val === "" || val.length &lt; 3) return; if(testObj.results.length) { for(var i=0, len=testObj.results.length; i&lt;len; i++) { var opt = $("&lt;option&gt;&lt;/option&gt;").attr("value", testObj.results[i]['city']); tempObj[testObj.results[i]['city']] = testObj.results[i]['id']; dataList.append(opt); } } }); </code></pre> <p>with ajax (does not work while typing):</p> <pre><code>$('.controls').on("input click", "#inputCity", function(e) { var val = $(this).val(); dataList.empty(); if(val === "" || val.length &lt; 3) return; $.ajax({ type: "GET", url: GET_cities, data: {startswith: val, maxRows: 5}, success:function(data){ if(data.results.length) { for(var i=0, len=data.results.length; i&lt;len; i++) { var opt = $("&lt;option&gt;&lt;/option&gt;").attr("value", data.results[i]['city']); tempObj[data.results[i]['city']] = data.results[i]['id']; dataList.append(opt); } } } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload