Alex. S. Alex. S. - 1 year ago 222
Javascript Question

How to set an event handler in a Django form input field

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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download