javascript - Why does the jquery selector ("id,id") selects all elements with duplicate IDs -
i came across year old code written developer (yes, knew him personally) access elements having same id.
$("#choice,#choice")
it returns elements having id. if use below
$("#choice")
it returns first match, expected.
after searching time, i'm unable figure out official links pointing technique, how selected elements duplicate id.
can please explain how working ?
update
please see question not alternative use. i'm aware of classselectors , attributeselectors , know having duplicate ids not recommended, have live years old code way (if know mean).
if @ code of sizzle.js jquery uses selecting elements based on selector understand why happens. uses following regex match simple id, tag or class selector:
// easily-parseable/retrievable id or tag or class selectors rquickexpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
but selector in question $("#id,#id")
not match selector , uses queryselectorall
(line no 270 in ref link), replaces selector "[id='" + nid + "'] "
(line no 297 in ref link) selects elements matching id.
however, agree other people in thread, not idea have same id multiple elements.
Comments
Post a Comment