最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you get just the parent element of a click event when event.target is returning the children? - Stack Overfl

programmeradmin4浏览0评论

JavaScript

function productBox(event){
    event.stopPropagation();
    console.log(event.target);
}
var product = document.getElementsByClassName('product');
for (var g = 0, length = product.length; g < length; g++){
    console.log('here');
    product[g].addEventListener('click',productBox);
}

HTML

<div class="product">
    <div class="productContent">
       <img src="file:///C|/Users/Jacob/Downloads/12939681_1597112303636437_733183642_n.png" />
    </div>
    <div class="productName">
         Here
    </div>
    <div class="productDescription">

    </div>

So the problem lies in the fact that when the product element is clicked, event.target returns the actual child element of the event listener. For example, i click a "product" and it'll return productContent, productName or productDescription as the target, when actually what i want is the "product" element and then to do a .childNodes and find the image within that.

Please note jQuery is not an option, it is 30kb of stuff i won't use as this is a static html page with barely any javascript.

I've thought perhaps,

doing a check if the element is 'product' if not, take the parent and check if it's a 'product', if not go to that parent and so on. Then find the img tag within that. But i feel like that is a long winded work around.

Any thoughts?

JavaScript

function productBox(event){
    event.stopPropagation();
    console.log(event.target);
}
var product = document.getElementsByClassName('product');
for (var g = 0, length = product.length; g < length; g++){
    console.log('here');
    product[g].addEventListener('click',productBox);
}

HTML

<div class="product">
    <div class="productContent">
       <img src="file:///C|/Users/Jacob/Downloads/12939681_1597112303636437_733183642_n.png" />
    </div>
    <div class="productName">
         Here
    </div>
    <div class="productDescription">

    </div>

So the problem lies in the fact that when the product element is clicked, event.target returns the actual child element of the event listener. For example, i click a "product" and it'll return productContent, productName or productDescription as the target, when actually what i want is the "product" element and then to do a .childNodes and find the image within that.

Please note jQuery is not an option, it is 30kb of stuff i won't use as this is a static html page with barely any javascript.

I've thought perhaps,

doing a check if the element is 'product' if not, take the parent and check if it's a 'product', if not go to that parent and so on. Then find the img tag within that. But i feel like that is a long winded work around.

Any thoughts?

Share Improve this question asked Apr 16, 2016 at 13:27 user3940738user3940738 3
  • 3 This or currentTarget don't work? developer.mozilla/en-US/docs/Web/API/Event/currentTarget – Dave Newton Commented Apr 16, 2016 at 13:35
  • 2 this gives you the bound element. – user1106925 Commented Apr 16, 2016 at 13:35
  • Thank you. this works. Completely forgot that this is where it's called not defined. Want to put as an answer so i can accept? – user3940738 Commented Apr 16, 2016 at 13:37
Add a ment  | 

4 Answers 4

Reset to default 6

To get the element to which the handler is bound, you can use this within the handler.

As @DaveNewton pointed out, the event object has a .currentTarget property that can be used as well. This is nice because you can have functions that have a manually bound this using .bind() or you may be using the new arrow functions, which have a this defined by its original environment, making it impossible to get the bound element that way.

You can use parentElement property of the target.

function productBox(event){
    var target = event.target;
    var parent = target.parentElement;//parent of "target"
    //Rest of your code
}

I asked a similar question: if you have a element.click(function(){}) and the element has two or multiple siblings. If the event trigger is attached to the parent and not the target, this post popped up and is actually irrelevant to my question so I decided to post my search here for someone else.

I used this method:

if (target.closest('.NEftune').attr('rel') != undefined){
 /*do something here*/ 
}

The closest method in jQuery and JavaScript starts from the clicked target and then bubbles up until it finds an attribute you looking for. In my case the event was attached to an Element with the class (.NEftune) and by adding an extra attribute I could determine if I was inside the container (.NEftune) which has an image inside.

I know it's a bit late, but the simplest solution to get the actual target to which the event handler was attached to is to use the event currentTarget property.

This will avoid unnecessary further DOM check and works out of the box on all modern browsers.

Event.currentTarget on MDN

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target, which identifies the element on which the event occurred and which may be its descendant.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论