Skip to main content

jQuery ajax this.id undefined



I have a list of items I delete using AJAX.





This list is a simple list with divs and each div as an id so when the item is removed from the database I return true and then it removes the line.





Here my code:





HTML







<div id="row1">

<div>item1</div>

<div><a href="...">view</a></div>

<div><a id="1">delete</a></div>

</div>







JS







$('.delete').click(function () {

if (!confirm('Are you sure you want to delete?')) {

return false;

}

$.ajax({

type: "POST",

url: '/delete_record',

data: 'id=' + this.id,

cache: false,

success: function (result) {

if (result == 'good') {

$('#row' + this.id).remove();

}

}

});

});







For some reason the this.id does not work because this.id is undefined ... why? I have id="1" on my a href.



Source: Tips4all

Comments

  1. I don't see any element with a class of 'delete' in your html. Assuming it's the delete link, you need to get the id with $(this).attr('id') instead of this.id.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?