Using form.element to show hide form element

90 views
Skip to first unread message

José Eloy

unread,
Aug 27, 2015, 10:40:08 PM8/27/15
to web2py-users
Hi!

Is it possible to use form.element to show/hide form elements?

I'd like to use:
            form.element("#id_element")['_show'] = True
to show an element
or
            form.element("#id_element")['_show'] = False
to hide an element

Thanks.

Anthony

unread,
Aug 27, 2015, 11:19:32 PM8/27/15
to web...@googlegroups.com
What are you trying to achieve? Do you want fields to be shown/hidden dynamically in the browser based on the values of other fields? If so, see http://web2py.com/books/default/chapter/29/07/forms-and-validators#Conditional-fields or use custom Javascript.

If you want to hide a given field when creating the initial HTML rendering of the form, then you can use something like your approach, though you would add a special CSS class:

form.element("#id_element").add_class('hidden')

You would then of course need a CSS definition for the "hidden" class (Bootstrap already includes such a class).

Anthony

José Eloy

unread,
Aug 29, 2015, 1:44:58 PM8/29/15
to web...@googlegroups.com
Thanks Anthony for your answer.

Yes, I want to show/hide elements dinamically based in values of other fields, I know I can do this using javascript or css, but I'd like to use either


form.element("#id_element")['_show'] = True
or
form.element("#id_element")['_show'] = False

Is it possible? Is there another way?

Regards.

Anthony

unread,
Aug 29, 2015, 3:55:05 PM8/29/15
to web2py-users
All Python gets executed on the server before the page is returned to the browser, so it is not possible to use Python to change HTML dynamically in the browser based on user input. This must be done via JavaScript. However, you can use the built-in conditional fields functionality (check the link I included), and the JavaScript will be handled for you by web2py.

Anthony

José Eloy

unread,
Aug 29, 2015, 7:34:18 PM8/29/15
to web2py-users
Hi Anthony.

I understand. Thanks for telling me
about conditional fields, I'll try them.

Regards.
Reply all
Reply to author
Forward
0 new messages