Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  178] [ 4]  / answers: 1 / hits: 22759  / 15 Years ago, sat, november 7, 2009, 12:00:00

How to set a JavaScript function as handler in the event onclick in a given field of a Django Form. Is this possible?



Any clue would be appreciated.


More From » django

 Answers
12

If you are looking for automating this process, you can create a custom widget for the field. In the widget class you will define a render method in such a way that it will also return the event binding code.



class CustomWidget(forms.TextInput):

class Media:
#js here
js = ('js/my_js.js',)

def render(self, name, value, attrs = None)
output = super(CustomWidget, self).render(name, value, attrs)
#do what you want to do here
output += ...
return output

[#98365] Wednesday, November 4, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
danalexc

Total Points: 114
Total Questions: 119
Total Answers: 103

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;