I had a crazy (but so crazy that it just might work) idea where every element on a page is created in javascript but given an ID which is a hash of its path in the DOM.
So instead of searching through the DOM to find an element, you hash the path and then getElementById() with that hash.
Problem with this is getting the path might be more expensive than searching the DOM in the first place. Any ideas how this could be done or if it is just plain stupid?
If you know the full path of the element, is going to that path actually hard enough to justify this sort of thing? Usually it’s things like collecting elements by common aspects unrelated to their absolute path that’s a hassle. And with engines like Sizzle out there, I’m not sure I’m seeing the use-case for this. 🙂
I also wonder how you would handle it when moving elements around. Re-assign their hashes, presumably, which starts getting ugly fast.
Note that your approach doesn’t only work with elements created at runtime by Javascript; you can assign them after the fact easily enough with a recursive descent function:
That’s a one-time hit on pre-generated content, rather than always having to re-generate content. Not advocating, just pointing out.