0

Is this possible with php and javascript? Can I echo my anchor tag ID and retrieve it with jquery onclick event? Echo " a href = 'example.con' id=' ".$row['userid']."' example /a"; now in jquery/javascript I say something like onclick of the anchor tag I get the id of whatever the row is. If not can someone give me another solution.

user3046739
  • 103
  • 1
  • 15

2 Answers2

1

This should do what you're looking for:

$('#myLinkId').click(function() {
    var linkId;
    linkId = $(this).prop('id');
    alert(linkId);
});

This code: .prop('id'); is specifically what gets the link's ID.

Sean
  • 626
  • 5
  • 12
  • @Satpal so it does! I wasn't even aware of that. Satpal's solution shoudl have better performance. Although, the jQuery way can have its uses too, for example, looping through a string list of properties. – Sean Feb 12 '14 at 20:47
  • Your method is correct. I have just suggested that you can also use it. – Satpal Feb 12 '14 at 20:50
0

Non jQuery alternative:

function getId(e){
    //IE e.srcElement.href);
    //Other e.target.href);
    alert(e.srcElement.id);
}

Add the onclick="getId(event)" to your anchor.

For crossbrowser implementation you can see this question:

How can I make event.srcElement work in Firefox and what does it mean?

Community
  • 1
  • 1
Allende
  • 1,480
  • 2
  • 22
  • 39