Assume the element you want to give a unique id to is stored in the var element, unless you are creating an element more than once every millisecond, you can use the following...

element.id = 'element-' + $time();

But a better way to do it would be to use the element's UID which it gets upon being extended (when using the $ function)...

element = $(element);
element.id = 'element-' + element.uid;

since the uid property is internal, pretend i didn't tell you about it :P
- Tom

On Oct 29, 2008, at 9:20 AM, Vortexmind wrote:


Hi
as I'm using mootools library to create new elements in my DOM tree,
I've made a small function to return a new ID and ensure that it's not
already defined in DOM tree. Here is it

function __getUID(){
        var rand_letters =
['a ','b ','c ','d ','e','f','g','i','l','m','n','o','p','q','r','s','t','u','v','z'];
        var uid = null;

        while(true){
                uid = rand_letters.getRandom() + $random(0,20) +
rand_letters.getRandom() + $random(0,20) + rand_letters.getRandom() +
$random(0,20);
                if(!$defined($(uid)))break;
                }
        return uid;
};

well ... it's kinda paranoid and not so efficient but it works. Does
anyone knows a better method to do this?


Reply via email to