0

I don't know why if I have only one text field for categories the autocomplete is working (i can see suggested fill when user is typing in that categories text input box) but when I want to use more than one field let's say for subcategories it's not working on both (suggestions are not displaying)... Please help

<script type="text/javascript">


        $.getJSON(  {{ route('search.categories') }}, function( data ) {

            var categories = data.map(function(val){
                return val.title;
            });

            auto(categories);

        });
        $.getJSON( {{ route('search.subcategories') }}, function( data ) {

            var subcategories = data.map(function(val){
                return val.title;
            });

            auto(subcategories);

        });

        function auto(categories){

            $("#category_input").autocomplete({
                source: categories,
                minLength: 2
            });
        }
        function auto(subcategories){

            $("#subcategory_input").autocomplete({
                source: subcategories,
                minLength: 2
            });
        }




    </script>

View:

<input type="text" id="category_input" />
<input type="text" id="subcategory_input"/>
3
  • 1
    You cannot have two functions with the same name (auto). More specifically, the second one overwrites the first one. Commented Aug 19, 2018 at 9:19
  • @Jeto I thought "auto" is not a name but kind of a type like en.cppreference.com/w/cpp/language/auto Commented Aug 19, 2018 at 9:25
  • 1
    Well maybe in C++, but not in JS :) Commented Aug 19, 2018 at 9:27

1 Answer 1

1

Try this:

<script type="text/javascript">


    $.getJSON(  {{ route('search.categories') }}, function( data ) {

        var categories = data.map(function(val){
            return val.title;
        });

        auto(categories);

    });
    $.getJSON( {{ route('search.subcategories') }}, function( data ) {

        var subcategories = data.map(function(val){
            return val.title;
        });

        auto_sub(subcategories);

    });

    function auto(categories){

        $("#category_input").autocomplete({
            source: categories,
            minLength: 2
        });
    }
    function auto_sub(subcategories){

        $("#subcategory_input").autocomplete({
            source: subcategories,
            minLength: 2
        });
    }




</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it's working but i had to delete ";" there ` }) auto(categories)`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.