Steve Workman's Blog

Unique IDs in AJAX Web Applications

Posted on by Steve Workman About 1 min reading time

This week, Roger Johansson of 456 Berea Street posted about unique IDs in web applications. I read this and thought, "you're right, they should be unique, but what if you've got an AJAX repeater?" By this I mean when I'm loading functional parts of my application that I'll be referencing with JavaScript again, do I have to maintain a unique ID? Surely it knows what I added last or how to make them into an array?

This problem is common with certain kinds of forms where you want to let the user add another item but don't want to have a large form on a page. For this I then append a bit of my form to the page. However, if I want to manipulate that field again, I have to assign it a unique ID, but what should that be?

Say the field had and ID of "cheese", what would you add to the end to make it unique? Would you continually increment and hope they don't hit the button 65 million times? (unlikely I know, but possible). Do you assign it a random number, then have to remember this random number and hope it doesn't come up again?

What I would love to see is to be able to (naturally) access arrays of IDs, allowing IDs to be duplicated without the browser falling over. Is it too much to ask, or do I have to keep appending random numbers to my fields?