1

I'm able to make a div tag using the document.createElement('div')

However i do not know how to give it a unique id.

can anyone help me with this.

I know how to do it using innerHTML however it very cumbersome. (I heard it not a good way of creating a layout.)

2
  • Is the focus here on unique as in a random-generated number, or how to cleanly assign an id to the div without going into innerHTML as you mentioned? Commented Sep 13, 2011 at 19:08
  • Did none of these answers help you? If one did, click the checkmark next to it. Commented Sep 16, 2011 at 20:06

4 Answers 4

9

Understanding unique as an ID that must not get mixed up with any other ID's in the markup, the easiest way to go is to get the local timestamp. As shown here:

let div = document.createElement("div");
// With old JS syntax
div.id = "div_" + new Date().getTime().toString();
// With ES6 Template Strings you can also write
div.id = `div_ ${new Date().getTime().toString()}`;

Though working with createElement can be a bit of a troublemaker, you should be using some JavaScript framework that solve the tiny little details for you (such as jQuery, Mootools, Dojo, etc.).

Sign up to request clarification or add additional context in comments.

2 Comments

I like this test, but I'm worried it's not quite good enough. When I run this back to back, I get two values that are identical. In my "live" test, I got ~6ms differnce in some cases, which is rather close.
This might be overkill, but I'm using this: var datestr = new Date().getTime().toString(), randomstr = Math.random().toString(); return string + '_' + MD5(datestr + randomstr); MD5 is a function from webtoolkit: webtoolkit.info/djs/webtoolkit.md5.js
4
var d = document.createElement('div');
d.id = 'myElementId';
d.innerHTML = 'This is the content!';

Or

var d = document.createElement('div')
   .id = 'myElementId';

.. same thing really, just a different way of laying it out.

This takes care of assigning the id. Now, for unique. The best way is to use the current time, as it isn't likely to repeat since javascript time is on miliseconds.

Putting it together:

var d = document.createElement('div')
   .id = 'id'+new Date().getTime().toString();

This does have the chance to duplicate itself in the right circumstances. If it is is hyper-critical to maintain uniqueness, then the only way is to use a pointer:

// establish  a variable to hold the pointer
var uniquePointer = 0;

// every time you use the pointer to create a new element, increment the pointer.
var d = document.createElement('div')
   .id = 'id'+uniquePointer;
uniquePointer ++;

2 Comments

I'll remove it now you've updated your answer, but it was a completely valid downvote and should be take as constructive criticism
I'll take it how I want to, and we now have a generation-spanning blood feud. I shall commence indoctrinating my children within the hour; prepare yourself.
2

You can use a library for this: https://github.com/LiosK/UUID.js

It "Generates RFC 4122 compliant UUIDs."

Having the element you can assign it the id using the code:

element.id = "somePrefix" + UUID.generate()

Comments

-1
var myDiv = document.createElement('div');
myDiv.id = "myUniqueId";

7 Comments

@adam: Changed it. How's that?
I've removed the downvote, but I think the question is more about how to generate a unique ID
@adam: Thanks, bro. I'm ok with you thinking that way. :)
No, @adam... stick to your guns! This is not a complete answer. "unique" means "not repeated", "different from others". If this code were used twice on the same page... not unique.
Sound the war drums, internet is serious business. (I've got a fiver that OP doesn't accept any answers anyway...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.