Madhu Dollu
Madhu Dollu

Reputation: 474

Assign the id as attribute to newly added row using jQuery ajax method in Codeigniter

I am using jQuery ajax method to insert the data to database.
Able to insert the data successfully, and returning the newly inserted id.

Here the problem is, I am appending the new row immediately after submit (prior to insertion of database)

Keeping in mind that, the data will be inserted from background, without causing any delay to the user.
He must be able to keep adding rows, and they must be saved to DB in background.

$('#add-new-row').live('submit', function(){        
   var obj = $(this),
   newdata = obj.find('.newdata');
   $('#target').append('<div>newdata.val()+'</div>');

   $.ajax({
       url: '/insert/insertrow',
       data: $(this).serialize(),
       dataType: 'json'
       // other settings
       success: function(response) {
           alert(response.id); // response contains the newly inserted id
       }
   });

}

But I need to attach the ID of new row to html for further processing.
Hence I am struck, to add this response.id to new row as one attribute.

In the above jQuery event I am appending the new row to the target as

<div>New row</div>

It should become something like this after it got inserted to database.

<div rowid="123">New row</div>

Any suggestions to achieve this scenario.

Upvotes: 0

Views: 391

Answers (1)

Smern
Smern

Reputation: 19076

How about putting this in the success:

 $("#target div:last-child").attr("rowid", response.id) 

Upvotes: 2

Related Questions