3

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>
ToddN
  • 2,901
  • 14
  • 56
  • 96

3 Answers3

12

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

$('.photosize').find('img:first').after('<br/><br/>');
Andy E
  • 338,112
  • 86
  • 474
  • 445
0

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

Chad
  • 19,219
  • 4
  • 50
  • 73
0

Do this (it's tested and works):

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

Hope this helps. Cheers

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61