Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set property 'url' of undefined

In the example below i create an instance of the typeahead class

    var ta = $.typeahead({
        input: '.super',
        minLength: 0,
        maxItem: 15,
        order: "asc",
        hint: true,
        href: "",
        searchOnFocus: true,
        display: ["name"],
        template: "{{name}}",
        source: {
            "SearchResults": {
                //ajax: {
                //    url: "/search/" + id,
                //    path: ""
                //}
            }
        },
        callback: {
            onClickAfter: function (node, a, item, event) {
                event.preventDefault;
                debugger;
            }
        },
        debug: true
    });

Later on, and by using the id variable which is obtained via the following method

    $("input").focus(function () { 
        id = $(this).attr("id");
        ta.source.ajax.url = "/search/" + id;
    }); 

i get the Cannot set property 'url' of undefined since the ta.source.ajax is actually null. How can i fix that?

Simply put, i do not want to create duplicate code for each input of my form

like image 668
OrElse Avatar asked Nov 28 '25 00:11

OrElse


1 Answers

Configure SearchResults group through options property of the Typeahead instance.

source property of Typeahead instance is generated from options.

ta.options.source.SearchResults.ajax.url = "/search/" + id;
like image 149
Oluwafemi Sule Avatar answered Nov 29 '25 15:11

Oluwafemi Sule



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!