Event.target does not return the exact item that was clicked

I am trying to use event.target to find the exact item that was clicked. I am working with jQuery Mobile and the links are on navbars.

When the page initially loads, event.target does the job and returns the correct 'a' element (there is a programmatic click that triggers this). However, when you click on the link, event.target returns the 'span' element, not the 'a' element that was clicked.

If you click the vicinity of the text portion of the link, event.target returns a valid "span" element.

I need event.target to return the exact "a" element that was clicked, whether you clicked directly on the link or not, if the user clicks on a given area of โ€‹โ€‹the link (including the "span" ').

You can see it in action here

Let me know if I need more information. Thank!


source to share

2 answers

Use this:

   var target=(event.target.tagName=='A')
                ? event.target
                : $(event.target).closest('a')[0]


it will return the A-element ascendent when you click on the child of the link

or simply: this



You can try event.currentTarget




All Articles