Reputation: 581
I am trying to figure out how to properly use the jQuery find()
method on an already existing matched elements set.
I initially retrieve a set of matched elements via
scrollItems = $('div.topicrow-header')
I am using a sticky
class to place a fixed header at top of page depending what content is currently in view.
When I detect a new element in view, I do
scrollItems.removeClass('stick');
$('#'+id).addClass("stick")
where id
is id of element that is currently in view.
Above works, but I really just want to only search the scrollItems
array directly as opposed to applying $('#'+id)
to the entire DOM
.
When I try
scrollItems.find('#'+id).addClass("stick");
it doesn't work. In Chrome Tools, I can see that scrollItems
is indeed the array I expect it to be, and that it contains an element with a matching id
, but above find
call is not returning anything.
I have also read that you can pass a second argument to jQuery
to limit the search context. So trying this
$('#'+id, scrollItems).find('#'+id).addClass("stick");
also doesn't work.
I have a feeling I am fundamentally not understanding some aspect of the scrollItems
data type, and that is why things are not working as I am aiming.
Upvotes: 1
Views: 55
Reputation: 4207
JQuery find is searching for child elements when indeed you seem to want to filter the array you already have. Use filter-method instead:
scrollItems.filter('#'+id).addClass("stick");
Upvotes: 2