Simon
Simon

Reputation: 1201

Add image after first image element jquery

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 :)

Upvotes: 2

Views: 5256

Answers (2)

Simon
Simon

Reputation: 1201

There is the answer to my problem, I resolved it by using this line :

$image.insertBefore("#container > img:last");

I remember that I add my pictures by the end and no the beginning of the container element.

Thanks again.

Upvotes: 0

Lix
Lix

Reputation: 47956

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")

Upvotes: 6

Related Questions