Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - conditionally append HTML

I'm trying to append a picture of a magnify glass after each image within a div. And some of them are linked and in that case I would like it to append it after the </a> that follows the img tag.

How can I make:

<img src="a.jpg" .... />

turn into:

<img src="a.jpg"><img src="mag.gif" />

And:

<a href="...."><img src="a.jpg" .... /></a>

turn into:

<a href="...."><img src="a.jpg"></a><img src="mag.gif" />

Note that there might be linebreaks before and after the img tags.

How is that done?

like image 743
Lasse Edsvik Avatar asked Jul 19 '26 09:07

Lasse Edsvik


1 Answers

You can use after method.

Insert content, specified by the parameter, after each element in the set of matched elements.

$('img').after('<img src="mag.gif"/>')
like image 184
undefined Avatar answered Jul 20 '26 21:07

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!