0

I have a code block in my jsp page. I want use this code in ajax with append tag. Here is my code.

for ( var i = 0, len = data.length; i < len; ++i) {
  var listValue = data[i];
  $('.flowers').append(
    '<div class="profile-sidebar" style="width: 250px;">\
      <div class="portlet light profile-sidebar-portlet" \
        data-category="listValue.officeNameNoSpace"> \
        <div class="profile-userpic"> \
          <img src="listValue.imgPath" class="img-responsive" \
          alt=""> \
        </div> \
        <div class="profile-usermenu"></div> \
      </div> \
    </div>'
  );
}

But it still has error.Here is error :

String literal is not properly closed by a matching quote

I tried this way.:

for ( var i = 0, len = data.length; i < len; ++i) {
  var listValue = data[i];
  $('.flowers').append(
    '<div class="profile-sidebar" style="width: 250px;">
      <div class="portlet light profile-sidebar-portlet"
        data-category="listValue.officeNameNoSpace">
        <div class="profile-userpic">
          <img src="listValue.imgPath" class="img-responsive"
          alt="">
        </div>
        <div class="profile-usermenu"></div>
      </div>
    </div>'' +
');

What should i do? Thanks.

jahller
  • 2,705
  • 1
  • 28
  • 30
Veysel
  • 71
  • 1
  • 2
  • 10

1 Answers1

0

It's a bit old fashioned but why don't you try string concatenation like below:

for ( var i = 0, len = data.length; i < len; ++i) {
                var listValue = data[i];
                $('.flowers').append('<div class="profile-sidebar" style="width: 250px;">'+

                        '<div class="portlet light profile-sidebar-portlet"'+
                            'data-category="'+listValue.officeNameNoSpace+'">'+
                            '<div class="profile-userpic">'+
                                '<img src="'+listValue.imgPath+'" class="img-responsive"  alt="">'+
                            '</div>'+

                            '<div class="profile-usermenu"></div>'+
                        '</div>'+

                    '</div>');
        }

Tell me if it works.

Rahul Singh
  • 892
  • 9
  • 24
  • Actually it worked so thank you. but also i cant use ( Listvalue.attribute ) with this way. I tried i.e. 'data-category="'+listValue.imgPath+' but it didnt work . – Veysel Oct 09 '15 at 06:40
  • I have edited my answer and it should work. Actually what you tried is correct src="'+listValue.imgPath+'" should work correctly. There must be some other error. You should try looking at the browser console to find the error detail. – Rahul Singh Oct 09 '15 at 11:19