Django – get value of a form textinput on onchange event

I am using this to get the ID and the value of a select item in a form in django with the onchange event:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class forms_bdc(forms.ModelForm):
[...]
bdc_description_1 = forms.ModelChoiceField( widget=forms.Select(attrs={'id': 'editable-select-2','onchange': 'populate_selected_product(this.id,this.value)'}),empty_label=None )
</code>
<code>class forms_bdc(forms.ModelForm): [...] bdc_description_1 = forms.ModelChoiceField( widget=forms.Select(attrs={'id': 'editable-select-2','onchange': 'populate_selected_product(this.id,this.value)'}),empty_label=None ) </code>
class forms_bdc(forms.ModelForm):
[...]
    bdc_description_1 = forms.ModelChoiceField( widget=forms.Select(attrs={'id': 'editable-select-2','onchange': 'populate_selected_product(this.id,this.value)'}),empty_label=None )

I am trying to do the same with a TextInput widget.
The ID is returned as expected but I don’t know how to get the value :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> bdc_quantity_1 = forms.CharField( widget=forms.widgets.TextInput(attrs={'onchange':'calculate_price_of_product(this.id,this.value)'}))
</code>
<code> bdc_quantity_1 = forms.CharField( widget=forms.widgets.TextInput(attrs={'onchange':'calculate_price_of_product(this.id,this.value)'})) </code>
   bdc_quantity_1 = forms.CharField( widget=forms.widgets.TextInput(attrs={'onchange':'calculate_price_of_product(this.id,this.value)'}))

I also tried with this.text instead of this.value but no success.

Thanks

The behavior of the event is sometimes specific, especially in the case of onchange, because there is a race condition between the browser and your code to try to resolve the value.

The first approach is to use a Timeout before calling calculate_price_of_product. The delay can be 0, but the function will be called after the event. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>attrs={'onchange': 'let input=this; setTimeout(function() {populate_selected_product(input.id,input.value)},0);}'}
</code>
<code>attrs={'onchange': 'let input=this; setTimeout(function() {populate_selected_product(input.id,input.value)},0);}'} </code>
attrs={'onchange': 'let input=this; setTimeout(function() {populate_selected_product(input.id,input.value)},0);}'}

This is usuall practice, you can search here, on SO by “is setTimeout(fn, 0) useful?”

The other way in Django to do, what you want, is to use raw event change in js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class BdcForm(forms.Form):
class Media:
js = ('bdcs/js/onchange.js',)
...
bdc_quantity_1 = forms.CharField() # I dont use any widget
</code>
<code>class BdcForm(forms.Form): class Media: js = ('bdcs/js/onchange.js',) ... bdc_quantity_1 = forms.CharField() # I dont use any widget </code>
class BdcForm(forms.Form):

    class Media:
        js = ('bdcs/js/onchange.js',)
    
    ...
    bdc_quantity_1 = forms.CharField()  # I dont use any widget

and onchange.js seems like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>window.addEventListener('load', function () {
document.getElementById('id_bdc_description_1').addEventListener('change', function() {
console.log('You selected: ', this.id, this.value);
});
})
</code>
<code>window.addEventListener('load', function () { document.getElementById('id_bdc_description_1').addEventListener('change', function() { console.log('You selected: ', this.id, this.value); }); }) </code>
window.addEventListener('load', function () {
    document.getElementById('id_bdc_description_1').addEventListener('change', function() {
        console.log('You selected: ', this.id, this.value);
    });
})

I called it better way because in Django the window.onload event is often used, and in django-admin there are many js.scripts “in the box” running on window.onload.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật