Modify ↓
Ticket #5556 (closed bug: invalid)
Selector by ID with attribute behaves weirdly (and differently across browsers)
| Reported by: | alexvy86 | Owned by: | |
|---|---|---|---|
| Priority: | minor | Milestone: | 1.4 |
| Component: | unfiled | Version: | 1.3.2 |
| Keywords: | selector, cross-browser | Cc: | |
| Blocking: | Blocked by: |
Description
I was trying to determine if at least 1 checkbox in the page (all with the same name and id) is selected, using this code:
if ($("#dnisToReserve:checked").length == 0) {
alert("No numbers were selected."); return false;
}
I then found out that this SHOULD be wrong since a selector with # will only return 1 element (from the Documentation: "(#) Matches a single element with the given id attribute").
The documentation is right if I don't use the ":checked" attribute, but if I use it I get weird behavior.
- In IE, it seems that the first checkbox gets retrieved (by id, I guess), and then the ":checked" filter applied to it, so if it is checked then the condition in my code is false, but if I check any other checkbox and not the first, the condition is true (length only returns 1 or 0, depending on the state of the first checkbox).
- In Firefox, it seems to ignore the # selector completely, since the length property returns exactly the amount of checked checkboxes in the page (with that id/name, I didn't try combining checkboxes with a different id/name), no matter if the first one is checked or not.
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

Oh, if it serves any purpose, I did manage to do what I needed by using the following selector:
$(":checkbox[name=dnisToReserve]:checked").length