0

Is there a way I can use this autocomplete for multiple textbox elements. The struggle I am facing is that I want to dynamically change the element binding and the source based on the textbox which is calling the function.

$("#element").autocomplete({
    source: "../ajax/autocompletes/trucks.php",
    minLength: 0,
    select: function (event, ui) {
        $("#element_id").val(ui.item.id);
    },
    change: function (event, ui) {
        if (!ui.item) {
            this.value = "";
            alert('PLEASE SELECT AN ITEM FROM DROPDOWN!');
        }
    }
}).dblclick(function () {
    $(this).autocomplete("search");
});

I have searched google but haven't found much.

2
  • Make a function and pass id and source as a parameters, then call it !!! Commented Jan 28, 2016 at 10:14
  • Oopps..!!!!! Very silly me!! Thanks Commented Jan 28, 2016 at 10:17

1 Answer 1

1

Try this and let me know , if you need further assitance

function multiAutocomplete(element,sourceUrl){
        $(element).autocomplete({
        source: sourceUrl,
        minLength: 0,
        select: function (event, ui) {
            $("#element_id").val(ui.item.id);
        },
        change: function (event, ui) {
                    if (!ui.item) {
                        this.value = "";
                        alert('PLEASE SELECT AN ITEM FROM DROPDOWN!');
                    }
                }
            }).dblclick(function () {
                $(this).autocomplete("search");
            });

    }

Call like this:

    multiAutocomplete('#element','../ajax/autocompletes/trucks.php');
    multiAutocomplete('#element2','../ajax/autocompletes/trucks2.php');
    multiAutocomplete('#element3','../ajax/autocompletes/trucks3.php');
Sign up to request clarification or add additional context in comments.

Comments

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.