0

I want to wrap entire content into single div, it needed for loop inside append function. after wrapping around div in javascript, it's not displaying anything.

here is my code;

$('#postjson').append(
  '<div>' + '<div id="' + data[0].id + '">' + '<p>' + '<div id="namediv">' + data[0].FirstName + '&nbsp;' + data[0].MiddleName + '&nbsp;' + data[0].LastName + '<br/>' + '<span id="locationspan">' + 'xyz' + data[0].Location + '</span>' + '</div>' + '<br/>'
  //+'Email:'+data[0].Email+'<br/>'
  //+'Mobile:'+data[0].Mobile+'<br/>'
  + '</p>' + '</div>'
  // displaying book details
  +
  for (var i = 1; i < data.length; i++) {
    tr = $('<tr/>');
    tr.append("<td>" + data[i].BookNo + "</td>");
    tr.append("<td>" + data[i].BookTitle + "</td>");
    tr.append("<td>" + data[i].BookGenre + "</td>");
    tr.append("<td>" + data[i].BookWriter + "</td>");
    tr.append("<td>" + data[i].BookDescription + "</td>");
    $('#displaytable').append(tr);
  } + '</div>'
);
5
  • Why use TR in a div? Where is the table? Also } + '</div>' is not valid JS. Your code is not recommendable and hard to maintain Commented May 24, 2016 at 13:10
  • table is outside the script , i am just adding rows to the table here. then please give some alternative solution Commented May 24, 2016 at 13:11
  • if you add an element distinct of a TR to the table, here is a DIV with tr inside, wont work, only tr elements will be recognized Commented May 24, 2016 at 13:13
  • I assume he wants to append displaytable to inside the div appended to postjson - it is horrible to read Commented May 24, 2016 at 13:14
  • 1
    A tr inside a div is absolutely invalid HTML markup. Commented May 24, 2016 at 13:15

2 Answers 2

1

Perhaps you meant

var $table = $('<table>', {"id:":"displaytable"});
for (var i = 1; i < data.length; i++) {
    tr = $('<tr/>');
    tr.append("<td/>",{"text":data[i].BookNo});
    tr.append("<td/>",{"text":data[i].BookTitle});
    tr.append("<td/>",{"text":data[i].BookGenre});
    tr.append("<td/>",{"text":data[i].BookWriter});
    tr.append("<td/>",{"text":data[i].BookDescription});
    $table.append(tr);
}

$('#postjson')
  .append($('<div />',{"id":"tableDiv"}))
  .append($('<div />',{"id":data[0].id}).append($('<div />',{"id":"namediv"}).html(data[0].FirstName + '&nbsp;' + data[0].MiddleName + '&nbsp;' + data[0].LastName + 
  .append($table);
Sign up to request clarification or add additional context in comments.

Comments

0

Put the loop outside of the append function and finally append the table to the 'container' element, which is specified in the html string

$('#postjson').append(
  '<div id="container">' + '<div id="' + data[0].id + '">' + '<p>' + '<div id="namediv">' + data[0].FirstName + '&nbsp;' + data[0].MiddleName + '&nbsp;' + data[0].LastName + '<br/>' + '<span id="locationspan">' + 'xyz' + data[0].Location + '</span>' + '</div>' + '<br/>' + '</p>' + '</div>')

for (var i = 1; i < data.length; i++) {
  tr = $('<tr/>');
  tr.append("<td>" + data[i].BookNo + "</td>");
  tr.append("<td>" + data[i].BookTitle + "</td>");
  tr.append("<td>" + data[i].BookGenre + "</td>");
  tr.append("<td>" + data[i].BookWriter + "</td>");
  tr.append("<td>" + data[i].BookDescription + "</td>");
  $('#displaytable').append(tr);
}
$("#container").append($('#displaytable'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.