0

How to avoid duplication of data using jquery? My code is :

<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 i clicking the button, the same data were shown. How to avoid it? sqlfiddle: http://jsfiddle.net/etdx5o08/

12
  • 1
    what duplication u want to avoid? Commented Apr 23, 2015 at 6:55
  • I think this is what you are talking about - jsfiddle.net/etdx5o08/1 Commented Apr 23, 2015 at 6:58
  • please check your updated fiddle jsfiddle.net/etdx5o08/2 Commented Apr 23, 2015 at 6:59
  • You're not duplicating any data... you are adding 5 NEW rows every time. If you don't want to do that, either don't add 5 new rows every time (only do it once), or remove all the rows before adding new ones. Commented Apr 23, 2015 at 6:59
  • @Hamza Kubba:You said actually I want. Plz give me a demo. Commented Apr 23, 2015 at 7:07

3 Answers 3

3

Do this:

$("#gen").click(function() {
       $("#report").parent().show();
       var rows='';
       for (var i = 0; i < 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>

Sign up to request clarification or add additional context in comments.

5 Comments

How is this any different from code above ? I think Jack is talking about duplication of Sl.No
@VigneswaranMarimuthu It's different because it uses .html() which replaces all of the html in the element where as the OPs original code simply uses .append() to add each row to the element so when he clicks a second time, it appends the rows again under the first set duplicating the data...
@DelightedD0D: Your code works fine. But in my question , at first, table was hidden and when the button clicked it must be visible.
@jackrose Ah, sorry, I removed that when testing and forgot to put it back, edited above
@DelightedD0D: Thanks.. This is what I needed.! Thanks for spending time for me.
0
$("#gen").click(function(){
    $("#report").css("visibility","visible");
    var table = $("#report tbody tr");
    for(var i=0; i<5; i++){
        var row='<tr><td>('+(table.length+1+i)+')</td></tr>'
        $("#report").append(row);
    }
});  

Demo

Comments

0

To avoid duplicates, you can simply update js as below.

$("#gen").click(function(){
$("#report").css("visibility","visible");
$("#report").html('<tr><th>Sl.No.</th><th>District Name</th></tr>');
for(var i=0; i<5; i++)
    {
        var row='<tr><td>('+(i+1)+')</td></tr>'
        $("#report").append(row);
    }
});

Just adding the below line before loop starts.

$("#report").html('<tr><th>Sl.No.</th><th>District Name</th></tr>');

Here's jsFiddle.

Hope it helps :)

2 Comments

This will add <tr><th>Sl.No.</th><th>District Name</th></tr> every time the button is clicked.
@VigneswaranMarimuthu no it wont, it will replace the element's html with <tr><th>Sl.No.</th><th>District Name</th></tr> every time the button is clicked, which solves the problem

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.