Bug Tracker

Opened 14 years ago

Closed 14 years ago

#4644 closed bug (invalid)

index() successively returns -1 after first time while a result should have been found

Reported by: Kennisnet Owned by:
Priority: undecided Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:


When executing this code:

jQuery.fn.test = function() {

return this.each(function() {

var $select = $(this); var $options = $select.find('.options'); var options = $options.find('li');




$(function() {



on a document with this HTML snippet (see attached file for working example)

... <div class="select">

<div class="options">


<li><a href="#">Option 1</a></li> <li class="active"><a href="#">Option 2</a></li> <li><a href="#">Option 3</a></li> <li><a href="#">Option 4</a></li> <li><a href="#">Option 5</a></li>



</div> <div class="select">

<div class="options">


<li><a href="#">Option 1</a></li> <li><a href="#">Option 2</a></li> <li><a href="#">Option 3</a></li> <li class="active"><a href="#">Option 4</a></li> <li><a href="#">Option 5</a></li>



</div> ...

'1' and '-1' are alerted, while I would have expected '1' and '3'.

Is this correct behaviour or possibly a bug?

Attachments (1)

test.html (1.1 KB) - added by Kennisnet 14 years ago.

Download all attachments as: .zip

Change History (2)

Changed 14 years ago by Kennisnet

Attachment: test.html added

comment:1 Changed 14 years ago by dmethvin

Resolution: invalid
Status: newclosed

You have *two* elements that contain a child with class "selected", but your .index() only searches for the child in the first "select". If you need more help with the logic, ask in one of the jQuery forums. That's the best place to start before filing a bug.

Also, I strongly recommend against what you seem to be doing here -- reinventing standard user interface controls from scratch. Web browsers do a lot of things for free that this code/markup does not, such as supporting ARIA, screen readers, and keyboard access. If you want to embellish a select, start with a real select in the markup and use progressive enhancement.

Note: See TracTickets for help on using tickets.