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
  • 679
  • 1
  • 10
  • 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,357
  • 8
  • 29
  • 53
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,444
  • 1
  • 10
  • 26
0

Use append instead of html

$('#wrapper').append('&lt;img src="profile.png"/&gt;');
Nagaraj S
  • 12,994
  • 6
  • 31
  • 51
asim-ishaq
  • 2,170
  • 5
  • 29
  • 55