Can you target an elements parent element using event.target?

JavascriptHtml

Javascript Problem Overview


I am trying to change the innerHTML of my page to become the innerHTML of the element I click on, the only problem is that i want it to take the whole element such as:

<section class="homeItem" data-detail="{"ID":"8","Name":"MacBook Air","Description":"2015 MacBook A…"20","Price":"899","Photo":"Images/Products/macbookAir.png"}"></section>

Whereas the code that i have written in javascript:

function selectedProduct(event){
  target = event.target;
  element = document.getElementById("test");
  element.innerHTML = target.innerHTML;
}

will target the specific element that i click on.

What i would like to achieve is when i click on anywhere in the <section> element, that it will take the innerHTML of the whole element rather than the specific one that i have clicked.

I would presume it is something to do with selecting the parent element of the one that is clicked but i am not sure and can't find anything online.

If possible i would like to stay away from JQuery

Javascript Solutions


Solution 1 - Javascript

I think what you need is to use the event.currentTarget. This will contain the element that actually has the event listener. So if the whole <section> has the eventlistener event.target will be the clicked element, the <section> will be in event.currentTarget.

Otherwise parentNode might be what you're looking for.

link to currentTarget
link to parentNode

Solution 2 - Javascript

To use the parent of an element use parentElement:

function selectedProduct(event){
  var target = event.target;
  var parent = target.parentElement;//parent of "target"
}

Solution 3 - Javascript

handleEvent(e) {
  const parent = e.currentTarget.parentNode;
}

Solution 4 - Javascript

function getParent(event)
{
   return event.target.parentNode;
}

> Examples: >1. document.body.addEventListener("click", getParent, false); returns the parent element of the current element that you have clicked.

> 2. If you want to use inside any function then pass your event and call the function like this : > function yourFunction(event){ var parentElement = getParent(event); }

Solution 5 - Javascript

$(document).on("click", function(event){
   var a = $(event.target).parents();
   var flaghide = true;
    a.each(function(index, val){
       if(val == $(container)[0]){
           flaghide = false;
        }
    });
    if(flaghide == true){
        //required code
    }
})

Solution 6 - Javascript

var _RemoveBtn = document.getElementsByClassName("remove");
for(var i=0 ;  i<_RemoveBtn.length ; i++){
	_RemoveBtn[i].addEventListener('click',sample,false);
}
function sample(event){
	console.log(event.currentTarget.parentNode);	
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamView Question on Stackoverflow
Solution 1 - JavascriptdonnywalsView Answer on Stackoverflow
Solution 2 - JavascriptKJ PriceView Answer on Stackoverflow
Solution 3 - JavascriptDavidView Answer on Stackoverflow
Solution 4 - JavascriptAbdul AlimView Answer on Stackoverflow
Solution 5 - JavascriptShubham RungtaView Answer on Stackoverflow
Solution 6 - JavascriptVanga Kiran Kumar ReddyView Answer on Stackoverflow