0

I have string:

var items = "<div class='item'><div class='item-img' style='background-image: url('images.123.jpg')'></div></div>"

I want to change inner html of div:

$('#div').html(items);

So the result would be:

<div id="div">
    <div class='item'>
         <div class='item-img' style='background-image: url('images.123.jpg')'>  </div>
     </div>
 </div>

How I can do that? Thanks a lot!!

Ormoz
  • 2,975
  • 10
  • 35
  • 50
David Sýkora
  • 574
  • 1
  • 3
  • 16
  • Cange it to `... style=\"background-image: url('images.123.jsp')\")>...` because if you use the same tick (') for `style=''` and `url ('')`, the first tick in url will close the tick from style=. The backslash in `\"` should quote the double quotes, so it doesn't close the quotes from you variable assignment. – Alexander May 25 '15 at 21:09
  • @Alexander `.jsp` <> `.jpg`! hahaha – mathielo May 25 '15 at 21:19
  • 1
    @mathielo typical typo for a Java Frontend developer ^^ – Alexander May 26 '15 at 09:42

2 Answers2

0
var div = $('<div>').attr("id","div").html(items);
$("body").append(div);
user3227295
  • 2,168
  • 1
  • 11
  • 17
0

The problem is that you are breaking the attribute style with quotation marks right here:

[...] style='background-image: url('images.123.jpg')'> [...]

See? The supposed-to-be inner ' actually breaks the style='' marks. As they are not mandatory for the CSS attribute, you could either not use them

var items = "<div class='item'><div class='item-img' style='background-image: url(images.123.jpg)'></div></div>"

OR you could escape double quotation marks:

var items = "<div class=\"item\"><div class=\"item-img\" style=\"background-image: url('images.123.jpg')\"></div></div>"
Community
  • 1
  • 1
mathielo
  • 6,725
  • 7
  • 50
  • 63