2

Maybe it is just a simple mistake but this thing is driving me crazy. I want to know why the hell this jQuery line is returning -1?

$('td[class!="traco"]:last').index('td.diaNormal')

First of all, when this function returns a negative number? Why does it happen? Thank you all in advance, Fernando.


EDIT:

The most intersting thing is that my page has the element i am looking for, and the jQuery index just returns -1.


EDIT2: This is my HTML, http://jsfiddle.net/bv7SU/

6
  • 1
    With what HTML is this being run? Can you post a live demo? Commented Mar 20, 2012 at 16:42
  • I am afraid the HTML is too big to post here, Commented Mar 20, 2012 at 16:56
  • 2 things... 1. your fiddle doesn't seem to be working... 2. your first 2 divs have the same ID... that's not the best idea when developing HTML DOM elements... IDs really should be unique... Commented Mar 20, 2012 at 17:16
  • I edited my answer... I added your jQuery code to your fiddle when the DOM was ready... and it seems to be working... I added a link to the new fiddle in my answer Commented Mar 20, 2012 at 17:19
  • 1- JSFiddle is working just fine, i did not put the css style, but thats pretty much it. Commented Mar 20, 2012 at 17:39

4 Answers 4

5

It is pretty standard in programming models when doing searches / finds that return indexes to indicate a "not found" with a negative number of minus one (-1) (naturally, because the indices will start from zero (0)). So, what you're seeing is the result of a successful call (in the sense that it worked) that just didn't find anything to match your criteria.

If this wasn't the case, how exactly would you determine whether something was found or not? There often needs to be a distinction between "this worked!", "this worked, BUT...", and "this failed!".

Sometimes there will be an alternative, such as a method just so named and documented to behave almost the same the other, but with the difference of "throw an exception on 'worked, BUT'", which is also often useful. But you generally need to be on the lookout for the latter modes as and when you need them.

Sign up to request clarification or add additional context in comments.

1 Comment

That is a very detailed and useful answer for a person with the user name "Mr. Disappointment"! :)
2

I don't know if this is the answer for which you are looking, but according to the docs "index()" will return -1 if there jQuery cannot find any elements for which you are attempting to look.

EDIT: I've added your jQuery code as an onDomReady to your Fiddle and I get the index of 29. How you are running your jQuery code?

new fiddle

1 Comment

like i said before my friend, now i realize that the problem is the :last method intead of index(). Have no idea why it does not work here. :/
1

It returns -1 when an object matching the selector passed as .index(selector) is not found.

From the jQuery documentation for .index():

If a selector string is passed as an argument, .index() returns an integer indicating the position of the original element relative to the elements matched by the selector. If the element is not found, .index() will return -1.

Comments

0

The answer is in the documentation:

If a selector string is passed as an argument, .index() returns an integer indicating the position of the original element relative to the elements matched by the selector. If the element is not found, .index() will return -1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.