I have table which is bound dynamically:
<table id="test">
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
</table>
I want to remove duplicate table rows, producing a result like this.
<table id="test">
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
</table>
I'm trying to do it through this btnRearrange
click.
$('#btnRearrange').bind("click", function() {
// want some help hear
});
Thanks.
Try -
var seen = {};
$('table tr').each(function() {
var txt = $(this).text();
if (seen[txt])
$(this).remove();
else
seen[txt] = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="test">
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
<tr>
<td>test1
<td>
</tr>
<tr>
<td>test2
<td>
</tr>
</table>
Code is taken (and very slightly changed) from this question - JQuery: Remove duplicate elements?
$('#btnRearrange').bind("click", function() {
var contents = {}, text;
$("#test td").each(function() {
text = $(this).text();
if( !( text in contents ) ) {
contents[text] = true;
}
else {
$( this.parentNode ).remove();
}
});
});
jsfiddle: http://jsfiddle.net/jKs4k/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With