David
David

Reputation: 5214

How to assign unique DOM element ID

My GWT application creates text areas, each of which must have an ID in order to be useful to a third-party JavaScript library. I know how to assign an ID to a GWT widget; I'm after a good way of generating those unique ID's.

Upvotes: 11

Views: 8669

Answers (4)

Chi
Chi

Reputation: 23164

For GWT, take a look at HTMLPanel.createUniqueId

String id = HTMLPanel.createUniqueId();

Upvotes: 17

Julian Aubourg
Julian Aubourg

Reputation: 11436

Javascript:

var idIndex = 0;

function getNewId() {
    return "textGWT"+(idIndex++);
}

Java:

class IdMaker {

    private static int idIndex = 0;

    public static String generate() {
        return "textGWT"+(idIndex++);
    }
}

Upvotes: 1

harto
harto

Reputation: 90493

Java has a built-in class for unique ID creation: http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html

Another common way is by using a timestamp, i.e. System.currentTimeMillis()

Upvotes: 1

meder omuraliev
meder omuraliev

Reputation: 186562

I believe this would be what you need for unique identifiers ( using a timestamp and the 'widget-' namespace ).

'widget-' + (new Date).valueOf()

Upvotes: 1

Related Questions