Skip to content
Advertisement

jQuery selector for all in a div if class includes a certain string

I would like to be able to select both of these with a single css selector:

JavaScript

Ideally, something like “class includes ‘foo'” to capture both those images at once. I know i can use .children().last().remove() twice but I’d like to make sure my code is a bit more dynamic than that.

EDIT: realized I’m doing this on a click, so I’d like to be able to achieve the same thing with $(this) as my jQuery starting point.

EDIT: I hacked it with $("#" + $(this).attr("id") + " img.foo").remove() but maybe there’s something more elegant.

Advertisement

Answer

You can try this to select all images with class foo

JavaScript

Or like this to select images with class foo under div with id someId

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement