Add image after first image element jquery

append, appendto, image, insert, jquery

Solution

You have some malformed HTML there- I'm going to assume it was a mistake when copying the code...

Perhaps this will help -

$('<img src="foo.jpg" />').insertAfter("#gallery > img:first");

If you want to use the `insertAfter()` function, you have to be sure that you have selected the correct element. In this case it is

- the first `img` tag - `:first`

- in the first level of children `>`

- in the element with an id of `gallery`. - `$("#gallery")`

Problem

I'm trying to add images dynamically in a div element. So I have a block like that ``` <div id="gallery" > <img src="images/slide1.jpg" alt="Slide 1 /> <img src="images/slide2.jpg" alt="Slide 2 /> <img src="images/slide3.jpg" alt="Slide 3 /> <img src="images/slide4.jpg" alt="Slide 4 /> </div> ``` The idea is to insert image dynamically after the first image so my new image goes after the first image. I tried appendTo, append , after, insertAfter . Do you have any ideas ? Thanks in advance :)

Original source