He worked at the s
It Smells Like Suc
I'm wondering why
Recruiting, Placem
Sumo at Sea
Holding on for Dea
An example of lewd
It’s Been Real and
I’ve never seen a
Let's Make a Deal

We went back down
My tongue makes no
Want to See the El
You’re stuck in my
Prenuptial Escape
Our company was na
Now That's a Rewar
This is Why You Pl
This brings back m
We Did it Guys
aisnob.com/html/a-better-more-efficient-way-to-define-classes/ https://www.simonofun.com/article/a-better-more-efficient-way-to-define-classes As a quick fix you could simply assign the element to a temporary variable when it's needed for the selector, which doesn't get the element loaded into memory, just used temporarily for the document lookup. $('img.class').addClass('something'); //bad var img = $('img.class'); img.addClass('something'); //good $('img.class').removeClass('something'); //bad var img = $('img.class'); img.removeClass('something'); //good //etc A: $("img.something").removeClass("something") works fine. Tested on jQuery v1.4.1 $('img.something').removeClass("something")







The key is, your elements don't have the class yet. So jQuery doesn't apply the class to them. Also, just in case, use $('img.something').removeClass('something') for consistency. A: This question has nothing to do with "load order", so I'll ignore the rest of the answers, which do not address the question. The core of this question is "why does the jQuery selector find all images and not just ones which have already been given a class in this script"? In other words: How can the selector return elements that do not yet exist, since this is what the question is really about. The reason for this is because the selector will attempt to manipulate the items in the document, not merely enumerate the collection of objects matching the selector. So jQuery will use the selector to search through the objects in the document and then perform the requested manipulation on each one. If the selector matches no objects in the document, the matching method will throw an error. This is why the code doesn't work. The documentation for .find() says this (bold emphasis mine): For each element in the current set of matched elements, get the next sibling of each element in the set of matched elements, if any, and then for each sibling, get the next sibling of the sibling, if any, and so on, until there are no more siblings to get. This can be demonstrated very simply in the console: $("div").find(); // this will not error. the selector matches nothing. console.log($.type($())); // TypeError: $ is not a function console.log($()); // undefined, however jQuery is not undefined. $("div").find(); // error. the selector matched nothing. the object is not in the document, so the callback function does not have a matching item. $("div").find("div"); // now we have divs in our collection. this is because the selector matched at least one element in the document. $("div div").find("div"); // no longer an error. now that we have a match, jQuery will continue to enumerate through the elements in the collection $("div").find("div div"); // yet more errors. $("div").find("div div div"); // and then last error. again, because we have a match, not because we ran out of elements. This can be more clearly demonstrated when we use .not(). // works fine $("div").find("div div").not("div"); // works fine too $("div").find("div.someclass"); // works fine too $("div").find("img[alt=bar]"); // error $("div").find("img[alt=bar] img"); // works fine though $("div").find("div div"); So, to conclude, jQuery will try to filter the selected items from the document into a single set of results for manipulation, but jQuery will not get a selection if there are no matches. That is why the selector returns all matches in the document in the first place, and that is why the error is thrown when there are no elements matching the selector in the document. This is one way to get around it, and the other way, already demonstrated, is to filter out the elements which match the selector already before you try to manipulate them. This, of course, makes no difference for the case where there are no matches on the page. For completeness, here is a working example that illustrates the answer:

Find

Not

Not Here Either