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:

<div id= "someID">
  <img src="images/leave-alone.png" class= "leave-alone">
  <img src= "images/source.png" class= "foo bar">
  <img src= "images/other-source.png" class= "foo zip">
</div>

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

$("img.foo") 

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

$("#someID img.foo")
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement