Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django crispy form, hide/show fields in the template

i'm using Crispy-Form and Bootstrap within Django. It works very well. Now, i would like to have a field showing only when another field has input. Basically i've a multpile select list called A visible, and a text field, B, hidden. once the the user focus/select one or more value in A, B should become visible. And if none are selected it should become invisible.

Does cripsy form have this feature or the possibility to write the JS? Or do i've to write the JS in the html page where the form is rendered?

ciao

like image 797
EsseTi Avatar asked Nov 24 '12 13:11

EsseTi


1 Answers

I've done something similar once. I assigned a class 'hidden' to the inputs that you want to be initially hidden. This can be done by nesting the fields in a Div, and assigning a css_class. See http://django-crispy-forms.readthedocs.org/en/d-0/layouts.html#universal-layout-objects

Then use javascript to remove the 'hidden' class when a certain action occurs.

If you decide to use jQuery, you can use the following function: $("input[name='a_hidden_field']").removeClass('hidden')

like image 154
Teisman Avatar answered Oct 30 '22 19:10

Teisman