I want to get all the IDs that start with blblblb_ and run a separate JS function that I made. Here is what I have, it is only getting the first ID:
$(window).scroll(function() {
var test = $('div[id^="blblblb_"]').attr('id');
foo(test);
});
Any ideas what I’m doing wrong?
The problem is that attr gives you the id of the first element in collection and you’re only calling
fooonce anyway.Use each to execute a function for all elements of a jquery collection :
Based on your comment, if you have a growing collection of objects having this kind of id and wanting to be sure foo is only called once for each id, you might do this :
But I’m doubtful about the use case…