Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 799
David Avatar asked Sep 23 '09 04:09

David


4 Answers

For GWT, take a look at HTMLPanel.createUniqueId

String id = HTMLPanel.createUniqueId();
like image 176
Chi Avatar answered Nov 14 '22 03:11

Chi


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

'widget-' + (new Date).valueOf()
like image 23
meder omuraliev Avatar answered Nov 14 '22 04:11

meder omuraliev


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()

like image 39
harto Avatar answered Nov 14 '22 02:11

harto


Javascript:

var idIndex = 0;

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

Java:

class IdMaker {

    private static int idIndex = 0;

    public static String generate() {
        return "textGWT"+(idIndex++);
    }
}
like image 34
Julian Aubourg Avatar answered Nov 14 '22 03:11

Julian Aubourg