Reputation: 561
I've got a fairly straight forward question. Why can't I use $('a.view').attr('id')
(Ref //1 in code) in my click function? I tried it and it failed to work but this.id
works. I guess I primarily want to know the difference in the context of the code below:
displayRecord.php (The following link calls the click function):
echo '<td><a href="#" style="text-decoration: none;" id="'.$data['id'].'" class="view" ><input type="button" value="View" /></a></td>';
editTicket.php:
$('a.view').click(
function(e)
{
//1
var ticket_id = this.id;
dlg.load('displayRecord.php?id='+this.id, function(){
var escalationValue = '';
$.post('escalateValue.php',{post_ticket_id:ticket_id},
function(data) {
if (data == 'No'){
showCount();
}
});
dlg.dialog('open');
});
});
Upvotes: 0
Views: 432
Reputation: 73896
This this, see if you are getting any alerts or not:
$(document).on('click', 'a.view', function(e) {
alert($(this).attr('id'));
alert(this.id);
});
Upvotes: 1
Reputation: 64526
$('a.view').attr('id')
can match multiple elements, if you have multiple anchors with a view
class, so you will not necessarily get the clicked element if you use it within a click
event. this.id
refers only to the element that was clicked and would also be the fastest way, but to demonstrate you could also do:
$(this).attr('id'); // in the click event
Upvotes: 3