You can use a combined selector to get all div
elements with the classes thumbnail
and selected
then remove the selected
class from it.(Since you are again adding selected
to current thumbnail ignore that also using not()
)
$(".thumbnail").click(function() {
$('.thumbnail.selected').not(this).removeClass("selected")
$(this).addClass("selected");
})
Demo: Fiddle