0

In table

    <table class="table table-striped table-hover universal" style="margin-bottom: 0px;">

        <tbody>
            <span id="appointment_info">
                <tr>
                <td>loading...</td>
                </tr>
            </span>
            <tr>
                <td>12:30 pm</td>
                <td>Jenny Harris</td>
                <td>Exam Room 1</td>
            </tr>
        </tbody>
    </table>

I am replacing span id="appointment_info" using jQuery's .html() method.

Like below.

var html_str = "";
var apmts_len = apmts.length > 5 ? 5 : apmts.length;

for (var i = 0; i < apmts_len; ++i) {
    html_str += "<tr> ";
    var info = apmts[i];
    for (var j = 0; j < 3; ++j) {
        html_str = html_str + "<td>" + info[j] + "</td> ";
    }
    html_str += "</tr> ";

console.log(html_str); }

However, this failed. It prints everything in a row in one column, in an ugly format.

The generated html_str looks like <tr> <td>09:00 AM</td> <td>Richard Schwarm</td> <td>7</td> </tr>

How can I fix this?

3
  • 1
    your html is invalid... tbody can't have span as its child Commented Jan 22, 2015 at 5:57
  • That's helpful. What would you suggest then? Commented Jan 22, 2015 at 5:57
  • It shouldn't matter what the line break formatting is. The browser does not care what white space you put in your html strings. Commented Jan 22, 2015 at 5:58

1 Answer 1

1

Your html is invalid... tbody can't have span as its child, there is no need to use a span instead you can use replaceWith like

var apmts = [
  [1.1, 1.2, 1.3],
  [2.1, 2.2, 2.3],
  [3.1, 3.2, 3.3]
]

var html_str = "";
var apmts_len = apmts.length > 5 ? 5 : apmts.length;

for (var i = 0; i < apmts_len; ++i) {
  html_str += "<tr> ";
  var info = apmts[i];
  for (var j = 0; j < 3; ++j) {
    html_str = html_str + "<td>" + info[j] + "</td> ";
  }
  html_str += "</tr> ";
}

$('#appointment_info').replaceWith(html_str)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-striped table-hover universal" style="margin-bottom: 0px;">
  <tbody>
    <tr id="appointment_info">
      <td>loading...</td>
    </tr>
    <tr>
      <td>12:30 pm</td>
      <td>Jenny Harris</td>
      <td>Exam Room 1</td>
    </tr>
  </tbody>
</table>

Another option is to use a separate tbody to hold the dynamic content and assign the id to that tbody and set its content using .html()

var apmts = [
  [1.1, 1.2, 1.3],
  [2.1, 2.2, 2.3],
  [3.1, 3.2, 3.3]
]

var html_str = "";
var apmts_len = apmts.length > 5 ? 5 : apmts.length;

for (var i = 0; i < apmts_len; ++i) {
  html_str += "<tr> ";
  var info = apmts[i];
  for (var j = 0; j < 3; ++j) {
    html_str = html_str + "<td>" + info[j] + "</td> ";
  }
  html_str += "</tr> ";
}

$('#appointment_info').html(html_str)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table table-striped table-hover universal" style="margin-bottom: 0px;">
  <tbody id="appointment_info">
    <tr>
      <td>loading...</td>
    </tr>
  </tbody>
  <tbody>
    <tr>
      <td>12:30 pm</td>
      <td>Jenny Harris</td>
      <td>Exam Room 1</td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

2 Comments

The problem is, there are multiple rows, and thus multiple <tr> ... </tr> tags
Oh then I could assign id to <tbody>?

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.