I have below code to retrieve some text from a php document:
$.get("server/test.php", function(data){
$('#thumbdest').prepend(data);
// $('#thumbdest').html(data);
});
The commented line returns everything as expected, but the prepend data does not. It seems to strip HTML tags when inserted. I can’t use the commented line because I have to insert multiple items after each other. $('#thumbdest').html(data); replaces everything in the thumbdest div as expected.
Below is the HTML I should get when using prepend
<tr>
<td>
<a href="#modal" class="thumbnail tableimg" role="button" class="btn" data-toggle="modal">
<img src="assets/img/huvudbilder/thumbs/img.png" class="tableimg" alt="">
</a>
</td>
<td>Test1</td>
<td>Test2</td>
</tr>
This is what I actually get from using prepend
<a href="#modal" class="thumbnail tableimg" role="button" class="btn" data-toggle="modal">
<img src="assets/img/huvudbilder/thumbs/img.png" class="tableimg" alt="">
</a>
Test1
Test2
Which in turn screws up the whole table. All the table elements disappear, why is that?
EDIT:
When testing alert(data) after the prepend I get below:
<tr>
<td>
<a href="#editImage_1222866094" class="thumbnail tableimg" role="button" class="btn" data-toggle="modal">
<img src="assets/img/huvudbilder/thumbs/favicon32.png" class="tableimg" alt="">
</a>
</td>
<td>Test1</td>
<td>Test2</td>
</tr>
Structure of the #thumbdest:
<table class="table table-hover">
<p> </p>
<thead>
<tr>
<th></th>
<th>Namn</th>
<th>Tid</th>
</tr>
</thead>
<tbody id="thumbdest"></tbody>
<tbody>
<?php getImageList($conn); ?>
</tbody>
</table>
Here,
prepend()is working as it is supposed to, not to strip tags.What you need to do is to place the
<tbody>inside the<div id="thumbdest">and then change your following script line from$('#thumbdest').prepend(data);to$('#thumbdest tbody').prepend(data);Source: http://forum.jquery.com/topic/prepend-stripping-out-html-table-tags