I’ve got an img, which is wrapped into a div container. Now I want to add the img class to the surrounding div. How can I accomplish this with jQuery?
<div class="imagecontainer">
<div class="imagebackgroud"></div>
<div class="imageicon"></div>
<img class="aligncenter">
</div>
The .imagebackground and .imageicon divs can be ignored, I want to add the img class “.aligncenter” to the .imagecontainer div.
This assumes there is an image in the container.
This assumes the image has a class of aligncenter and just puts that same known class on the parent.
Or you can use find:
EDIT
Ok so according to your comment…
should work. It’s targeting all images with class “aligncenter” and then adding the class to the parent. While this looks very close to example 2, it is targeting specifically images with class aligncenter this way.
Here is a fiddle working:
http://jsfiddle.net/U5vRe/