Search code examples
javascriptdjangodjango-formsdjango-templatesdjango-autocomplete-light

Remove chosen value of django-autocomplete using script


I have assigned a value to the foreign key element (which is an autocomplete in the form) using initial. Coming to the front-end if the category is changed I used script to hide and show fields accordingly. So if I change the category and the auto-complete goes to hidden state I just want to remove its initial value.

I tried this:

...
$('#id_category').change(function(){
    var category = $('#id_category').val();

    if(category == 'customer'){
        $('#id_customer').show();
        ...
    } 
    else {
        $('#id_customer').val('').hide();
        ...
    }
});

any other way


Solution

  • $('#product option').val(null).trigger('change');
    

    or You can use the close button.To remove the value. This can only used for Customised Autocomplete.sample image

    $("span.select2-selection__clear").trigger('mousedown');