Unique IDs in AJAX Web Applications

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?

If you enjoyed this post, leaving a comment or subscribe to the RSS feed to have future articles delivered to your feed reader.

Steve Workman

Steve is an engineering manager at Maersk, and organises BerkshireJS. He has also worked at Yell and PA Consulting and is a former organiser of London Web Standards

More Posts - Website - Twitter

Tags: ,

One comment

  1. Richard Wilkinson said:

    You could always try css selectors, which will work with many of the javascript frameworks out there (prototype, jquery, etc). Then you can leave off the ids, use a unique css class for your repeaters, and do something like this (prototype):

    $$(‘some_css_class’) -> [dom elements]

Leave a response