Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Prepend an Image

I am trying to add a prepend of an image and then define it's attributes. Little confused as how to do this - currently I have the following but it's not working. The HTML is

<div id="testID" class="test1">
        <div id="testID2" class="test2" ></div>
    </div>

And the JS is

var test123 = somecode{}
jQuery(test123).find('#testID2').prepend('<img />').attr({
   src: 'some src.gif',
   alt: '',
   height: '60'
});

I am hoping to achieve

<div id="testID" class="test1">
  <div id="testID2" class="test2" >
      <img src='some src.gif' alt='' height='60' />
  </div>
</div>

Any Ideas?

like image 371
Tom Avatar asked Jun 24 '26 22:06

Tom


1 Answers

Should be:

jQuery("<img/>").prependTo("#testID2").attr({
   src: 'some src.gif',
   alt: '',
   height: '60'
});

because from your previous code, you are attaching the attributes to the container and not the img tag

like image 113
Heinrich Lee Yu Avatar answered Jun 27 '26 12:06

Heinrich Lee Yu