CodeArtist
CodeArtist

Reputation: 5684

Django: Is possible to execute javascript or jquery directly from Media subclass?

i have just a small django form which requires at creation time an execution of an equally small jquery code (just half of a line) and i was wondering if i can give this code of one line inside django, somehow... Is this possible? The line i want to execute looks like this:

$("#my_text_box").my_plugin_apply();

And i was thinking something like the following:

class MyForm(forms.Form):

    #Here for example only one input field...

    class Media:
        js = (
            'http://code.jquery.com/jquery-1.8.3.js',
            'http://code.jquery.com/ui/1.10.1/jquery-ui.js',
            'load_my_plugin_here.js',
            '$("#my_text_box").my_plugin_apply();',
            )

Thanks in advance!

Upvotes: 0

Views: 150

Answers (1)

Drachenfels
Drachenfels

Reputation: 3286

It's crazy and you should not do that, but you can actually.

class MyForm(forms.Form):

#Here for example only one input field...

class Media:
    js = (
        'http://code.jquery.com/jquery-1.8.3.js',
        'http://code.jquery.com/ui/1.10.1/jquery-ui.js',
        'load_my_plugin_here.js" onload="$("#my_text_box").my_plugin_apply();',
        )

I've tested that on django 1.4.5 and of course $(document).bind('ready', function() {}); might be usefull (didn't test that).

The more proper solution will be to add this line into it's own javascript and include that to your class or if you prefer modify html for admin that will execute line.

Upvotes: 1

Related Questions