First of all, if you mean to add the element attribute
id
to some automatically generated elements, you can always do it, I'm not sure this is the best technique you can use. First of all, why did not you generate the required IDs when you generated the table? Secondly, do you need to use
document.getElementById
? Is that your purpose? Instead, you could keep the references to all elements you need in memory and use it later. I hope you also know that with JavaScript, you can add any property to any object at any time. In other words, as you auto-generate some elements and add them to DOM, you can reference them outside DOM as general-purpose JavaScript objects. Just some food for thought.
Now, if you really need to use the
id
attribute of a DOM element, I would advise to use jQuery and the method
.attr()
. Please see:
http://api.jquery.com/attr/[
^],
http://jquery.com/[
^],
http://en.wikipedia.org/wiki/JQuery[
^].
Using jQuery really makes such things easy to implement and maintain; and the code looks very compact. Also, the library addresses browser compatibility problems. I highly recommend using this library when it comes to using JavaScript in browsers.
—SA