I've been really focused on javascript and improving performance for my website. One thing i do often is, create elements dynamically, and access these elements using ids.
Maybe you can help me with some questions.
What are the major draw backs for giving every interesting node in the document a unique id?
What is the ideal number of ids in a document?
Is there a maximum number of ids for a document?
In terms of performance, is getting an element by css class slower than that of getting it by id?
Thank you guys for your answers. If you have any additional notes to these questions about dom and accessing them, it would be appreciated.
Thank you.
The HTML id attribute is used to specify a unique id for an HTML element. You cannot have more than one element with the same id in an HTML document.
Use ids for uniquely tagged/identifiable items such as a particular button that will be used only once, a specific carousel element or any other graphical element. Use classes for elements that are more commonly used in your application such a div element with a specific height that will be displayed many times.
You can use ids more than once. The browser will still apply the styles. It is, however, not best practice to do so and is classed as invalid markup. To get around it you can add a class to the element.
IDs must be unique - the same ID should not be used more than once in the same page. Otherwise, they can interfere with the user agent's (e.g. web browser, assistive technology, search engine) ability to properly parse and interpret the page.
I know of no real penalty of using "id" values liberally, other than the one annoyance of IE, Safari, and Chrome promoting "id" values to window
properties. Good JavaScript code should be able to deal with that, however.
Note that:
In modern browsers, lookups by class can be pretty fast, but that's only because the work of doing it has been submerged into the low-level support code of the browser (possibly supported by more elaborate internal data structures, caches, etc). Now class names are also very important, both for simple semantic markup and for use by client-side code, so I'm not saying that classes are bad or anything. In fact there are times when doing things exclusively by "id" would be fairly stupid, when using classes would introduce simplicity.
edit — as of now (end of 2013) Firefox creates window
properties for elements with "id" attributes too. :(
Elements with IDs have some other non-javascript related advantages as well, such as hyperlinking to the element directly. In general, I prefer to avoid littering the markup with ids unless I feel it is truly necessary, for example: with dynamic content from a database that needs to interact with javascript.
To address your performance concerns, it's rare that an inefficient selector is going to give you significant problems. While it's good to optimize, I wouldn't go out of your way to give every element that you might select an id for this reason. Most performance issues are due to other things, like slow database queries while doing AJAX requests, or running lots of animations at once, or just inefficient javascript in general.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With