ToddN
ToddN

Reputation: 2961

jQuery append or appendTo after an img

I cannot get this simple append working. I'm trying to add two break tags AFTER the first Image.
This is a link

<script type="text/javascript">
$(document).ready(function(){

  $('.photosize').find('img:first').append('<br/><br/>');
});
</script>

Upvotes: 3

Views: 4644

Answers (3)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

Do this (it's tested and works):

$('.photosize img').first().after('<br/><br/>');

Hope this helps. Cheers

Upvotes: 0

Chad
Chad

Reputation: 19609

This already works, with caveats:

1) When you call $.append() it appends the string to the innerHTML of the element you are appending to. So for this instance it will add two line breaks to the innerHTML of the image element. Try using $.after() instead:

$('.photosize').find('img:first').after('<br/><br/>');

2) The :first selector doesn't work in many (if not all) versions of IE. You can fix this by selecting it using an ID or class instead of the pseudo class :first

Upvotes: 0

Andy E
Andy E

Reputation: 344517

append() inserts the elements as a child of the element it applies to. Use after() instead:

$('.photosize').find('img:first').after('<br/><br/>');

Upvotes: 12

Related Questions