Search code examples
jqueryinputautocompleteradio-button

jQuery autocomplete source parameter does not update


I'm using the jQuery autocomplete script for a text input element but the problem is that the lang variable in the source URI doesn't update when the variable is updated.

The value of lang is updated whenever I click on a radio input element and after that I type something in the text input element which then trigger the autocomplete code. Strangely enough it always uses the "en" value and not the updated value.

Does anyone know a better approach or a fix for my code?

code:

var lang = 'en';

$('input[name="language"]').click(function()
{
    lang= $(this).val();
});

$("#query").autocomplete({
    source: "domain.com/suggest.php?language=" + lang,
    minLength: 1
});

Solution

  • The solution is to a create custom AJAX call:

        source: function(request, response)
        {
            $.ajax({
                url: 'domain.com/suggest.php',
                dataType: "json",
                data: {language: $('input[name="language"]:checked').val(), term: $('#query').val()},
                success: response
            });
        },
    

    Hope this helps others too.