[Solved] Why can’t I get the index of a jQuery object?


I don’t think map() is giving you want you want.

Since you want to filter element from an array it’s easier to use filter() instead of map().

function $errorObjectFunction() {
    return $("div[id^=name]").filter(function() {
        return ($(this).find(":first-child").hasClass("error") == true  || $(this).find(".field_error").length > 0)
    });
}
var $self = $("#self");
var jdks = $errorObjectFunction();
var hdjs = $self.parent().parent().parent();
console.log("index: " + jdks.index(hdjs));

// logs "index: 3"

The problem you are having with map, is that returning $(this) is making your array a list of query objects rather than dom elements. I think the filter() approach is much nicer, but if you need to use map() for some reason use: return this instead to return the actual dom element.

4

solved Why can’t I get the index of a jQuery object?