Search code examples
javascriptjqueryhtml-tableprepend

Prepending a first row to table with jQuery


I have a table in my code:

<table class="mainTable">
</table>

I want to prepend a head row to it.

var filter_all_head_row="
            <tr>\
                <th>smth</th>\
                                ...
                    <th>smth</th>\
                        </tr>
";

I'm trying to do it in onload of my page with this code:

$("table.mainTable").prepend(filter_all_head_row);

By the console says:

Uncaught SyntaxError: Unexpected token ILLEGAL

Solutions?


Solution

  • You are missing at least two \ in your string literal:

    var filter_all_head_row="
        <tr>\
            <th>smth</th>\
            <th>smth</th>\
        </tr>
    ";
    

    Should be:

    var filter_all_head_row = "\
      <tr>\
        <th>smth</th>\
        <th>smth</th>\
      </tr>\
    ";