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.
Asked
Active
Viewed 83 times
0
-
4Can you try rephrasing that so that a person who isn't you might be able to understand it? – JohnFx Feb 12 '14 at 20:34
-
Yes, you can do that. – Kevin B Feb 12 '14 at 20:36
2 Answers
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?