Tihom
Tihom

Reputation: 3394

Invoke click handler on class selector and retrieve the ID

I have an HTML Table with each row:

<table>
<tr><td><a href='#' id='1' class='delete'>delete</a></td></tr>
<tr><td><a href='#' id='2' class='delete'>delete</a></td></tr>
<tr><td><a href='#' id='3' class='delete'>delete</a></td></tr>
</table>

I need a jQuery function that gets the id when the click handler is called:

$('.delete').click(function() {
  //Get id and set to num
  var num = XXXXX //ID
  //Invoke ajax request using num
  jQuery.ajax({type:'POST',data:{'num': num}, url:'/user/delete',success:function(data,textStatus){deletePicture(num);;},error:function(XMLHttpRequest,textStatus,errorThrown){}});
});

Two questions: 1) How do I get the ID 2) Is the ajax success callback correct?

Upvotes: 1

Views: 202

Answers (2)

gansbrest
gansbrest

Reputation: 817

var num = $(this).attr('id') I believe?

Upvotes: 0

David Tang
David Tang

Reputation: 93664

Your success callback looks fine (provided that deletePicture is defined elsewhere).

To get the id, you just need this.id:

var num = this.id;

Upvotes: 1

Related Questions