Behseini
Behseini

Reputation: 6330

Not Able to Add Dynamic Variable Into A Dynamic Table

Can you please take a look at this code and let me know why I am not able add the variable into the table?

var variable = "app ha";
var table = '<table class="table table-striped table-bordered"><tr><td>Energy Target</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Run of River Priorities:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Species:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Disturbance:</td><td><span class="'+ variable +'"></span></td></tr><tr><td>Slider:</td><td><span class="'+ variable +'"></span></td></tr></table>';

 $("body").append(table);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

Upvotes: 3

Views: 49

Answers (2)

Duncan
Duncan

Reputation: 226

Instead of putting your variable in the span class add it to the inner HTML of the span tags.

    var variable = "app ha";
    var table = '
 <table class="table table-striped table-bordered">
  <tr>
   <td>Energy Target</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Run of River Priorities:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Species:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Disturbance:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
  <tr>
   <td>Slider:</td>
   <td><span class="">'+variable+'</span></td>
  </tr>
 </table>';

     $("body").append(table);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

Your variable is succefully added as class attribute, if you want to show it inside tds add it also inside column :

var table = '<table ....
<tr>
    <td>Energy Target</td>
    <td><span class="'+ variable +'">'+ variable +'</span></td>
</tr>
.....</table>';

Hope this helps.

Upvotes: 1

Related Questions