Check if an element exists after an image

Hi,

I’m using a WYSIWYG editor on a site. As you know, they can be a big random for how they display things. So I’m attempting to clean it up a bit.

What I need is to check and see if anything (could be text, image, embed, etc) exists AFTER an image. If it’s not possible to check if all or any of those things exists after an image, then I could settle for simply checking if text If something exists after an image, then addclass to said img. If nothing exists after image, then add a different class to said image.

An example would be like this:

<div>
some text
<img class="text-after-img">
some text
</div>

and

<div>
some text
<img class="no-text-after-img">
</div>

I was able to get a little help elsewhere on this but I’m not sure it’s a complete solution:

if (!!($this.html().indexOf('>') === ($this.html().length - 1))) {
...doSomething...
}

That will check and see if the last element inside of the parent has a closing “>” which would work for images and this does work, but the situation could arise that the last element may have a closing “>” but not be an image, e.g.,

Thoughts?

The item after the image is its next sibling(DOM elemen’s property.“nextSibling”).
In JQuery, the function name is next();

$(".no-text-after-image").next()

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.