Question

I studied the Crispy-Forms documentation and I tried to put an extra button into one of my forms. With

self.helper.add_input(Button('back', "Back", css_class='btn'))

I can add a nice button. But the Button() wont take an onclick or on_click-attribute. So how can I add logic to this button? Adding an onclick event with JQuery isnt a very nice solution...

Thanks!

Ron

Was it helpful?

Solution

This is not included by default (afaik..). If you just need this once, is possible to use crispy-forms HTML Layout Object

HTML('<input type="button" name="Save" onclick="do_whatever" />')

What do you then dislike using jQuery? You could handle this rather easy and generic, using something like:

$('form :submit.ajax_submit').live('click', function(e) {
    e.preventDefault();
    var my_form = $(this).parents('form');

    // do whatever
    alert(my_form.attr('id'));
    alert(my_form.attr('action'));
});

and then just pass the class:

Submit('save', 'save', css_class='ajax_submit')

OTHER TIPS

Are you certain that Button will not take onclick in its kwargs?

I just added an onclick="javascript here" to a Submit() element and it displayed fine.

I also peered a bit at the underlying code, and I think all inputs by default flatten the kwargs that aren't popped because of special use (i.e. template), and pass those through into the rendered HTML. This may be new since April '12 (when originally posted), but it currently seems to be as simple as the following:

self.helper.add_input(Button('back', "Back", css_class='btn', onclick="alert('Neat!');"))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top