3 ms·
For the first problem, you could test the generated selector to verify it matches only one element. If not you can use, for example, the nth-child pseudo-class
by gildas 14y ago
For the first problem, you could test the generated selector to verify it matches only one element. If not you can use, for example, the nth-child pseudo-class to select the right element.
For the second one, specs say that the "id" value must be unique in the entire document [2].
[1] http://www.w3.org/TR/selectors/#structural-pseudos http://www.w3.org/TR/selectors/#structural-pseudos
[2] http://www.w3.org/TR/html401/struct/global.html#h-7.5.2 http://www.w3.org/TR/html401/struct/global.html#h-7.5.2
- arunoda 14y agoThanks. First one seems nice. I'll implement that. But if the content is changed dynamicly we might face some issues. BTW: in reality developers do not adhere to [2] and there is no any browser restrictions for that. So I have to think beyond the spec.
- gildas 14y ago> But if the content is changed dynamicly we might face some issues. Regardless the problem I was raising, if the document changes dynamically, your generated signature has great chances to fail (cf. also comment from bdunn). > in reality developers do not adhere to [2] I really hope it's not true because it would really be quite pointless. Let's try by yourself: put the same id name (e.g. "testId") on two different elements in your example and then try this code: $("#testId") It will return only one element. So you'll never be able to find the second one with a selector...
- arunoda 14y ago>Regardless the problem I was raising, if the document changes dynamically, your generated signature has great chances to fail (cf. also comment from bdunn). Yes :) >It will return only one element. So you'll never be able to find the second one with a selector... Thanks. I was not aware of that. Now this encourage me even more :) If a bad programmer like me place two ids with same name. Extension developer like x did not able to get the second one. That why I need this
- arunoda 14y agoupdated the source according to the first recommendation.