javascript - Avoid duplication using jquery -
how avoid duplication of data using jquery? code :
<button id="gen">generate</button> <table id="report" border="1" style="visibility:hidden"> <tr> <th>sl.no.</th> <th>district name</th> </tr> </table>
js:
$("#gen").click(function(){ $("#report").css("visibility","visible"); for(var i=0; i<5; i++){ var row='<tr><td>('+(i+1)+')</td></tr>' $("#report").append(row); } });
each time when clicking button, same data shown. how avoid it? sqlfiddle: http://jsfiddle.net/etdx5o08/
do this:
$("#gen").click(function() { $("#report").parent().show(); var rows=''; (var = 0; < 5; i++) { rows = rows+'<tr><td>(' + (i + 1) + ')</td><td></td></tr>' } $("#report").html(rows); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="gen">generate</button> <table border="1" style="display:none"> <thead> <tr> <th>sl.no.</th> <th>district name</th> </tr> </thead> <tbody id="report"></tbody> </table>