Alina Anjum Alina Anjum - 5 months ago 7x
Javascript Question

How to know that the anchor was on text or image?

I was suffering from the problem of getting anchor text and print it on the redirected screen in my question:
How to get anchor text in a Session variable?
I got the solution from this question but now if the anchor is on the image,How can i pass some text with it to print it on the redirected page ?
Someone Kindly review the question above in link and help me please.
Thanks in advance


You can use data- attributes:

<a class="customLink" href="your/url" data-text="yourText"><img src=".." alt=".." /></a>

and send the url with the text from the data-text attribute:

$(document).on('click', '.customLink', function(e) {
   var that = $(this);
   var url = that.attr('href');
   var text = that.attr('data-text');
   // Continue here ...