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/
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.