Hi,

I'm coming across something a bit unusual that I'm not sure if anyone's seen before.

I'm basically trying to accomplish the same thing this post is doing:
http://www.felixeve.co.uk/drupal-form-with-dynamically-adding-a-collecti...

Having a button which dynamically generates a collection of textfields via an ajax callback.

I have an idea of what sort of causes it but don't really know how to avoid it. What I'm seeing is, when I click the button, the new fields are added, form rebuilds, etc. after that happens I see that the input tag for these fields have had it's original ID changed, for instance "edit-textfield-1-name--2" while originally it was just "edit-textfield-1-name". And of course it'll just keep incrementing while I continually click the button.

Obviously it must be something to do with the ones in the loop that were originally display during each consecutive callback request & rebuild but again I don't know how to make it stop doing that. It probably thinks all those unique IDs are going to be displayed in multiple places on the same page but they're not.

I would like for them to not change during the form rebuild so that I can do some javascript stuff to them and it'll just complicate things if they keep changing.

I've narrowed it down to the line of code on common.inc line 3979. Was this by design or is this some sort of bug?

Any advice would be appreciated.

Thanks

Comments

Jaypan’s picture

It's by design. Drupal tracks the elements that have had ajax applied to them, so it needs a unique ID each time in order to ensure that any ajax is applied to the new elements.

edward.radau’s picture

And I suppose there's no way to make it not do that? Especially for the ones that were rendered originally prior to each ajax callback?

I just want a reliable way to select them via jquery. I'll try adding a class to the ones I'm concerned about.

Jaypan’s picture

There is usually already a descriptive class attache to elements. If there isn't, then I suggest, as you've already figured out, to add one.