0

I have a table and I want to add a new row to this table. I load html of the row from the server. Returned html has some other commented out html on the top. Because of that comment adding row doesn't work properly. jQuery removes <tr> and <td> tags and adds plain text from <td> elements. Is it a bug or expected behaviour?

<table id="tbl">
    <tr><td>old1</td></tr>
    <!-- <abc -->
    <tr><td>old2</td></tr>
</table>
<input id="add" type="button" value="add"/>

JavaScript:

$("#add").click(function() {
    $("#tbl tr:last").after("<!-- <abc --><tr><td>New</td></tr>");  
});

fiddle

P.S. The issue is because of '<' in the comment. It works ok with normal comments. And it would also work with '<' if the comment was inserted into/from div elements rather than table/tr. What would be your suggestion to fix this issue without removing comment from the html on server-side.

7
  • Is the offending < in the comment always in the same position in the returned data? Commented Nov 30, 2013 at 22:51
  • in theory it is but I use that html on the server side as a template so I wouldn't want that modifying the template (moving the comment) affected the behaviour. Commented Nov 30, 2013 at 22:54
  • You use comments in any way after you insert them? Commented Nov 30, 2013 at 22:56
  • and normal comment is html comment <!--<form> --> Commented Nov 30, 2013 at 22:56
  • 1
    Do you need the comment added to the table? Commented Nov 30, 2013 at 23:01

3 Answers 3

1

You can simply remove comments by using regular expressions. Code(Demo on JSFiddle):

$("#add").click(function() {
    var rawHTML = "<!-- <abc --><tr><td>New</td></tr>";
    var HTMLToAdd = rawHTML.replace(/<!--[\s\S]*?-->/g, '');
    $("#tbl tr:last").after(HTMLToAdd);  
});

Shorter version:

$("#add").click(function() {
    $("#tbl tr:last").after("<!-- <abc --><tr><td>New</td></tr>".replace(/<!--[\s\S]*?-->/g, ''));  
});
Sign up to request clarification or add additional context in comments.

Comments

1

Referring to my comment above - e.g. the below JS works...

$("#add").click(function() {
    newRow = '<!-- <abc --><tr><td>New</td></tr>';
    var newRow = newRow.replace('<!-- <', '<!-- ');
    $("#tbl tr:last").after(newRow);    
});

where the newRow is actually the data returned by your server call.

1 Comment

It would work but my UI could break after the server side modifications even if server produces valid html
0

if you get always string like that and you don't want to have comment at all, you can strip it off

$("#add").click(function() {
    var responseString = "<!-- <abc --><tr><td>New</td></tr>";
    var index = responseString.indexOf("-->")+3;
    var formatedString = responseString.slice(index);
    $("#tbl tr:last").after(formatedString);  
});

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.