Say,I have 5 paragraphs in the page.
if I execute:
p_array=$('p');
second_p=$('p:eq(1)');
$.inArray(second_p,p_array);
I get -1. Any explanation?
Neither p_array nor second_p are arrays.
They are jQuery objects.
More specifically, p_array is a jQuery object containing a set of 5 DOM nodes. second_p is a jQuery object containing a set of 1 DOM node.
$.inArray can function on these jQuery sets of nodes, but you can't compare a set against a set.
If you extract that one DOM node using the array subscript operator (jQueryObj[i]), then you're no longer comparing a set against a set:
var p_array=$('p');
var second_p=$('p:eq(1)');
alert($.inArray(second_p[0], p_array)); // result: 1
See a live demo here.
second_pis a jQuery object, andp_arrayis an array-like object which contains DOM nodes... Therefore,p_arrayobviously does not containsecond_p.second_p = p_array.eq(1);? You're doing two look-ups unnecessarily.