Skip to main content

Bug Tracker

Side navigation

#5556 closed bug (invalid)

Opened November 26, 2009 06:02PM UTC

Closed November 27, 2009 04:07AM UTC

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:
Blocked by: Blocking:
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.
Attachments (0)
Change History (2)

Changed November 26, 2009 06:04PM UTC by alexvy86 comment:1

Oh, if it serves any purpose, I did manage to do what I needed by using the following selector:

$(":checkbox[name=dnisToReserve]:checked").length

Changed November 27, 2009 04:07AM UTC by dmethvin comment:2

resolution: → invalid
status: newclosed

The ID attribute must be unique in a document.