Let's say I have a simple DAL autocomplete
& form
rendering custom html results, where the result contain data-attribute.
from django.utils.html import format_html
class CountryAutocomplete(autocomplete.Select2QuerySetView):
def get_result_label(self, item):
return format_html('<span data-url="{}">{}</span>', item.get_dashboard_url(), item.name)
class CountryForm(forms.Form):
country = forms.ModelChoiceField(
queryset=Country.objects.all(),
widget=autocomplete.ModelSelect2(
url="country-autocomplete",
attrs={
"data-result-html": True,
},
),
)
Now on select I want to get the data attribute of the selected item.
$(document).ready(function() {
$('#id_country').on('select2:select', function(e) {
// get the selected item data attributes.
});
});
I've tried $(this).find(":selected").data('url')
. But it returns undefined