Bug Tracker

Ticket #11413 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

jQuery Attribute Selector Bug

Reported by: Mike Murray <charliebrown928@…> Owned by:
Priority: low Milestone: None
Component: selector Version: 1.7.1
Keywords: Cc:
Blocking: Blocked by:

Description

 http://jsfiddle.net/qD5Se/show/

At the above jsFiddle example, we have extremely simple markup:

<input tabindex="0" name="Username" />
<input tabindex="1" name="Password" />
<input type="hidden" name="Hidden" value="value" />
<input tabindex="2" type="submit" value="Submit" />
<a href="#" id="ForgotPassword">Forgot Password?</a>​

When I do the following jQuery selectors in the JavaScript console of Chrome Dev Tools or Firebug...

$('[tabindex="0"]')
$('[tabindex="0"]:visible')

...I get confusing results. The first selector does as I intended (i.e., select the single html element on the page that has an attribute of tabindex with a literal value of 0, which is the Username input text field). I would then expect the second selector to produce the same result, as it has the same selector but with one extra restriction that the element be visible. However, I actually get more results than the first, despite the more restrictive selector (i.e., it matches Username text field but also matches the ForgotPassword anchor link as well).

It appears what happens is that when the :visible selector is added, the attribute selector is not doing a literal match anymore. When I do the same attribute selector, but without quotes...

$('[tabindex=0]')
$('[tabindex=0]:visible')

...I now get all elements which effectively have a tab index of zero by default, not just the one that literally has the attribute in its markup (i.e., now we match the Username text field and the ForgotPassword link, and additionally the hidden input field in the case of the selector without the visibility check).

What I'm gathering is that when the :visible selector is appended to the attribute selector of [tabindex="0"], it switches to behaving like the [tabindex=0] attribute selector instead.

Change History

comment:1 Changed 3 years ago by sindresorhus

  • Priority changed from undecided to low
  • Resolution set to invalid
  • Status changed from new to closed
  • Component changed from unfiled to selector

This is not a jQuery bug.

tabIndex="0" means:

In tab order relative to element's position in document.

 https://developer.mozilla.org/en/Accessibility/Keyboard-navigable_JavaScript_widgets

You should begin your tabIndex with 1:  http://jsfiddle.net/mofle/qD5Se/1/

Note: See TracTickets for help on using tickets.