2

I want to set the id property of an input element of a form that I'm creating in a module using the form API. I'm doing this I looked up the documentation and it says this:

id

INTERNAL. Used to populate form elements' id property. In rare cases, you can set this value yourself on a form element, to override the default setting.

It doesn't really say when I can or can't use it, so I'm hesitant to try to do this, since I can't rely on it.

I'm writing some javascript that has the id of the element hard-coded into it. Should I use the Drupal-generated id in my script instead?

1 Answer 1

1

I would avoid changing that at all and use the Drupal-generated IDs in your Javascript. I have never been forced to do otherwise.

Keep in mind, however, that you'll have better luck if you use classes or if you prefer IDs that don't have a node ID or other sequence-based ID in them. For example, I try to avoid using IDs when theming webforms because if I create the webform on another site, the ID will change.

If there are existing styles for what you want to do, I would check which selectors they are using as well.

Remember that you can add your own classes with the theming layer and then you can use them for your Javascript or jQuery. Sometimes this is the best choice, and I've done it numerous times.

If worst comes to worst, implement hook_form_alter and change the #id in question. If your form continues working, then you're good.

4
  • So when I write my javascript, I hard-code the drupal-generated ID into it, since I can't get it in advance programmatically. The form rendering is not going to suddenly change the value for some reason under my nose, is it?
    – user1359
    Commented Nov 7, 2011 at 14:46
  • What form ID is it? Form IDs don't change unless the underlying form's name changes. If a module renames its form function, for example, that may cause a change. This is not likely to happen under your nose; if you are working with others, just let them know which form you themed. Also keep an eye on it when updating your modules. Other than that, you should be fine. I will update my answer with some additional tips. Commented Nov 8, 2011 at 3:36
  • It's not a form id, but an input element. Something <input type='text' name='whatever' id='this-is-what-my-js-references>` -- I need to make sure that that won't change.
    – user1359
    Commented Nov 8, 2011 at 14:10
  • Assuming this is part of a form...the two main things that will change it is if its array key changes in code (basically, it is renamed) or if it is put inside a fieldset. So if neither of those happen, you should be fine. If you have access to the code, I'd recommend just implementing hook_form_alter and changing #id to something static, then seeing if the form still works. Commented Nov 8, 2011 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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