I put in my template.php an hook_form_alter function where I assign '#type'= 'hidden' to "field_bergamo".

But I want to show it when in the select list "Provincia" of the same form I select the option "Bergamo".

I suppose to solve with jQuery, but in the inspector of the page I can't see the "#edit-field-bergamo" ID, only the name.

I tried "jQuery ('input[name="field_bergamo"]').show()", it doesn't work.

If I don't assign ['#type'] = 'hidden' to "field_bergamo" in hook_form_alter I can see the "#edit-field-bergamo" in the inspector and the jQuery commands .show() .hide() work correctly.

Why if I assign ['#type'] = 'hidden', the field doesn't have an ID and doesn't react to my jQuery commands?

Any solution?

P.S. I can't use Conditional Field because the select list "provincia" is inside a field type "Postal address" and is not singularly accessible.

And I don't like to install a module if not strictly necessary!


You've converted the element to a hidden input type.

Hidden inputs cannot be rendered, as the name suggests (they're hidden by their nature). So trying to use javascript to "show" them will never have any effect - there's nothing to show.

Add that to the fact that a changed hidden input element will invoke a validation failure for a tainted form, and it should be easy to see why the Drupal devs didn't bother to add an HTML ID to the element. Trying to access it with CSS/JS makes very little sense most of the time.

If you just meant to hide the original input, not convert it to a different input type, just use CSS to hide/show it as normal.


If I understand correctly, you want to show/hide certain form fields based on the value of another field. While this is not an exact answer to your question (Clive answered it already) and it was asked long ago, I would like to add the following.

If you're working with a node form, the easiest way to do that is usually the Conditional Fields module. It can be configured from the admin UI, so no programming is needed.

Under the hood, Conditional Fields uses the Form API States system (documentation | tutorial blog post) that is part of Drupal core. You can also add form states to a form in your custom hook_form_alter implementation achieve your goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.