user7596515
user7596515

Reputation:

How to loop rows in dynamically generated table

i am working on adding various insurance policies while adding user details.

like this:

enter image description here

And when user add insurance add insurance details, it will show their data in temporary table using jQuery append system

like this:

enter image description here

Now i am facing problem is that, when admin multiple insurance details then it not loop s. no. it always shows 1

here is my code

<script type="text/javascript">

                                    function add_ins(){




                                      var mode = $('#Mode').val();
                                      var term = $('#term').val();
                                      var mat_amt = $('#mat_amt').val();
                                      var premium = $('#premium').val();
                                      var ins_date = $('#ins_date').val();

                                      var id = 1;
                                      // alert(mode);

                                      $('#insurance_temp').append('<tr><td>'+id+'</td><td>'+mat_amt+'<input type="text" name="mat_amt[]" value="'+mat_amt+'"><input type="text" name="term[]" value="'+term+'"></td><td>'+ins_date+'<input type="text" name="ins_date[]" value="'+ins_date+'"></td><td>'+mode+'<input type="text" name="Mode[]" value="'+mode+'"></td><td>'+premium+'<input type="text" name="premium[]" value="'+premium+'"></td><td><a style="cursor: pointer;" data-toggle="modal" data-target="#notification'+id+'"><span class="fa fa-pencil"></span>&nbsp;Notfication</a></td><td>Edit</td><td>Delete</td></tr>');

                                      id++;

                                      $('#frm_add_insurance')[0].reset();

                                    }

i just want to append these records with different serial numbers. please suggest me where i am going wrong

Upvotes: 0

Views: 351

Answers (3)

Pranit Jha
Pranit Jha

Reputation: 157

You are assigning id = 1 and that's why you are getting same id all the times. Try This:

var id = ($('#insurance_temp tr').length + 1);

Upvotes: 0

hotstreak
hotstreak

Reputation: 1

It looks like you are setting the value of your id to 1 every time it runs your function. Try initializing your id outside of the function and just increment it within your function.

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

Beacuse each time of function call your defining var id = 1; inside the function so it's always 1

define the variable out side the function globally . so it will go 1..2..3.

Upvotes: 1

Related Questions