Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get event target's parent with jQuery?

I use jQuery's event object to get the target's parent. The code is:

event.target.parentElement 

In Chrome it works, but in Firefox, it doesn't. I use alert() to print the output.

event.target is an URL in both Chrome and Firefox; but event.target.parentElement in Chrome is [Element object], and in Firefox it is null.

What should I do to solve this problem? I still wonder if IE will have this problem?

Thanks.

like image 816
郡 简 Avatar asked Mar 19 '12 06:03

郡 简


People also ask

How do I create a target event in jquery?

The event. target property returns which DOM element triggered the event. It is often useful to compare event. target to this in order to determine if the event is being handled due to event bubbling.

What is the difference between event target and event currentTarget?

target is the element that triggered the event (e.g., the user clicked on) currenttarget is the element that the event listener is attached to.


1 Answers

To access the parent, you could do:

 var par = $(event.target).parent(); 
like image 199
Sudhir Bastakoti Avatar answered Sep 22 '22 11:09

Sudhir Bastakoti