0

I've got 3 nested divs.

<div id="wrapper">
   <div id="innerWrapper">
      <div id="inner"></div>
   </div>
</div>

I need to add html via jQuery./ So I use

$('#wrapper').html('<img src="profile.png"/>');

But when I do that the innerWrapper and inner divs disappears. How can I add <img src="profile.png"/> instead of replacing the divs inside wrapper?

Bekki
  • 719
  • 2
  • 12
  • 20
  • 2
    Use `append()`. `$('#wrapper').append('');` or `prepend()` if you want it before the `innerWrapper` – Tushar Aug 26 '15 at 10:18
  • 1
    `$('#wrapper').append('');` - There are multiple methods to do it.. but it really depends on where you want the new element to be added to – Arun P Johny Aug 26 '15 at 10:18

3 Answers3

0

you need to use .appand() instead of .html(), .html() will remove the previous elements.

$('#wrapper').append('<img src="profile.png"/>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">wrapper
   <div id="innerWrapper">innerWrapper
      <div id="inner"></div>inner
   </div>
</div>
ozil
  • 6,930
  • 9
  • 33
  • 56
0

Use append() instead of html()

$('#wrapper').append('<img src="profile.png" titile="image"/>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <div id="wrapper">
   <div id="innerWrapper">innerwrapper
  <div id="inner">inner</div>
   </div>
</div>
Sathish
  • 2,440
  • 1
  • 11
  • 27
0

Use append instead of html

$('#wrapper').append('&lt;img src="profile.png"/&gt;');
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
asim-ishaq
  • 2,190
  • 5
  • 32
  • 55