Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of element IDs you'd want on a page?

Having trouble searching on this. The only thing I've found so far was related to a mapping API where (for map markers) JavaScript bogs down badly around 500 IDs. I'm pretty sure that was related to the particular scripts in use though and not a general rule.

I have a page with a complicated list. Each item in the list has about 10 distinct IDs on it and there's several JavaScript scripts in play. The list is paginating and the user has a choice of how many items to show per page. Considering each item in the list has ~10 IDs, I'm wondering what I should set the maximum number of items per page to (total number of records the user can choose to view at one time on a page).

I mean - aside from increased load times based on the raw number of records, is there a known number of id's where a limit is hit or where CSS/JavaScript performance starts to degrade sharply?

Edit: Pardon, each 'item' is complex business card (to paint a picture) with three divs in play and a small form. That's whay each item (record) has about 10 id's.

like image 635
Reno Avatar asked Mar 18 '11 12:03

Reno


People also ask

How many times can ID attribute be used on a page?

An element can't have more than one ID and an ID can't be used more than once in a page.

How many times can Ids be used in HTML?

HTML id Attribute You cannot have more than one element with the same id in an HTML document.

Can you get multiple elements by ID?

As per the W3c spec, id attributes "must be unique within a document". That's the whole point of a unique identifier, and is why you don't have DOM methods to get multiple elements with the same ID (since the latter doesn't make any sense).

How do I select all elements by ID?

Use the document. querySelectorAll() method to get all elements whose id starts with a specific string, e.g. document. querySelectorAll('[id^="box"]') . The method returns a NodeList containing all the elements that match the provided selector.


2 Answers

I think you will find that it will ultimately depend on the user's browser and personal system.

What would probably make the most sense is to do some research on your intended user base, what browser and how much ram they have available. You can profile your application in your local environment (and there was already a discussion about doing just that). That would give you an idea of how many system resources you're using versus how much the user has available.

But given the amount of highly graphical javascript stuff going on with HTML5, ect., I would wager you're going to need to have a ridiculous amount of data displaying for the javascript performance to be your biggest sticking point (at least in the most modern browsers, if you have to support something like IE6, all bets are off).

Closed the parenthesis, sorry for the willie-giving

like image 96
dmcnelis Avatar answered Sep 28 '22 03:09

dmcnelis


You didn't answer my comment and you've already accepted an answer, but I'll give an answer anyway, because I've got a feeling you may not need all the IDs you have.

Let's take following example code:

<div id="card-1" onclick="doSomethingWithCard('card-1')">
   <h2 id="card-1-name">John Doe</h2>
</div>

Both IDs here can be unnecessary. Many people try to get a reference to an element, that triggered an event, by passing its ID to the event handler and using getElementById. Instead this already contains a reference:

<div onclick="doSomethingWithCard(this)">
   ...
</div>

And once you have that reference, you can use getElementsbyTagName, a CSS selector (usually with a JavaScript Framework/Library like jQuery) or XPath to access the elements inside instead of their separate IDs. This works best if all items have the same inner structur using the appropriate elements (h2, h3, ul, form, etc.) and classes.

jQuery example:

function doSomethingWithCard(card) {
   alert($(card).find("h2").text());
}
like image 30
RoToRa Avatar answered Sep 28 '22 02:09

RoToRa