datalist: improve example and pass input element to url function - jscancer - Javascript crap (relatively small)
 (HTM) git clone git://git.codemadness.org/jscancer
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit b4ee6b2cb8c463a9127067e53c704f81d39f4340
 (DIR) parent 19e7513dd8e720e3aff80e70cbcaf38608a3efd7
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Tue, 13 Jun 2023 22:41:10 +0200
       
       datalist: improve example and pass input element to url function
       
       Diffstat:
         M datalist/datalist.js                |       4 ++--
         M datalist/example.html               |       9 ++++++---
       
       2 files changed, 8 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/datalist/datalist.js b/datalist/datalist.js
       @@ -56,7 +56,7 @@ function datalist_init(input) {
                };
        
                if (url.length || urlfn.length) {
       -                urlfn = urlfn.length ? window[urlfn] : function(s) {
       +                urlfn = urlfn.length ? window[urlfn] : function(s, input) {
                                return url + encodeURIComponent(s);
                        };
        
       @@ -65,7 +65,7 @@ function datalist_init(input) {
                        datalist_match = function(s, fn, ev) {
                                clearTimeout(timer);
        
       -                        var requrl = urlfn(s);
       +                        var requrl = urlfn(s, input);
                                if (requrl === prevurl) {
                                        fn(prevmatches);
                                        return;
 (DIR) diff --git a/datalist/example.html b/datalist/example.html
       @@ -30,17 +30,20 @@
        <p>Using XMLHttpRequest + JSON:</p>
        
        <label for="remote">OS: </label>
       -<input type="text" placeholder="Select OS..." value="" data-url="example-data.json?q=" name="remote" id="os" class="datalist" /><br/>
       +<input type="text" placeholder="Select OS..." value="" data-url="example-data.json?q=" name="remote" id="remote" class="datalist" /><br/>
        
        <p>Using XMLHttpRequest + custom URL function + JSON:</p>
        
        <label for="remotecustom">OS: </label>
       -<input type="text" placeholder="Select OS..." value="" data-urlfn="custom_urlfn" name="remotecustom" id="os" class="datalist" /><br/>
       +<input type="text" placeholder="Select OS..." value="" data-urlfn="custom_urlfn" name="remotecustom" id="remotecustom" class="datalist" /><br/>
       +
       +<label for="remotecustom2">OS: </label>
       +<input type="text" placeholder="Select OS..." value="" data-urlfn="custom_urlfn" name="remotecustom" id="remotecustom2" class="datalist" /><br/>
        
        </form>
        
        <script type="text/javascript">
       -function custom_urlfn(s) {
       +function custom_urlfn(s, input) {
                return "example-data.json?q=" + encodeURIComponent(s);
        };
        </script>