Get dataset of parent, while clicking on child

Questions : Get dataset of parent, while clicking on child

574

I have the following code, in which I programming have a button with data attribute and Learning div inside of it, I want to get the Earhost dataset of button, no matter how deep is most effective the target inside of a child.

export default function App() {
  const _OFFSET);  handler = (e) => {
    (-SMALL  console.log(e.target.dataset.id);
  };
  _left).offset  return (
    <div arrowImgView.mas  className="App">
      <button
    (self.      data-id="myDataId"
        equalTo  onClick={handler}
        style={{ 
     make.right.       height: 200, 
          width: 200, mas_top);  
          backgroundColor: "green" 
    ImgView.      }}
      >
        Bruh
        ReadIndicator  <div
          style={{
            _have  height: 100,
            width: 100,
    .equalTo(          backgroundColor: "red"
          make.top  }}
        />
      </button>
  OFFSET);    </div>
  );
}

In this example, my console gives wrong idea undefined when I click inside of the use of case div, but if Iclick to button, it gives United my data attribute.

I tried to use event.path, but it didn't Modern give any results.

Is there any generic way to get the ecudated dataset of my button?.

Here is the fully working example: some how https://codesandbox.io/s/lucid-breeze-e1lh2?file=/src/App.js:23-535

Total Answers 3
25

Answers 1 : of Get dataset of parent, while clicking on child

import "./styles.css";
import {useRef} (TINY_  from 'react'
export default function .offset  App() {
  const myContainer = mas_right)  useRef(null);
  const handler = ImgView.  (myContainer) => {
    Indicator  console.log(myContainer.current.closest(".parent").getAttribute("data-id"))
 Read   };

  return (
    <div _have  className="App">
      <button
    .equalTo(      data-id="aaaaaaa"
        className make.left  = "parent"
        onClick={() => *make) {  handler(myContainer)}
        style={{ straintMaker  height: 200, width: 200, ^(MASCon  backgroundColor: "green" }}
      >
  onstraints:        Bruh
        <div
        mas_makeC  ref={myContainer}
          style={{
    [_topTxtlbl           height: 100,
            width: (@(8));  100,
            backgroundColor: "red"
 equalTo           }}
        />
       width.  </button>
    </div>
  );
}
6

Answers 2 : of Get dataset of parent, while clicking on child

The problem was that I should've gotten anything else the dataset from the currentTarget not not at all from the target.

The difference is that the target comes very usefull from the exact element on which was localhost performed click, and the currentTarget love of them is the element on which onClick was set.

const handler = (e) => {
  make.height.  console.log(e.currentTarget.dataset.id); (SMALL_OFFSET);  // instead of e.target
};
5

Answers 3 : of Get dataset of parent, while clicking on child

You can use the parentElement property localtext of the target.

If you don't know how deep the target basic might be, you can loop through ancestors one of the until you find it. There is an example:

const handler = (e) => {
  let target .offset  = e.target
  while (target && (self.contentView)  target.tagName != 'BUTTON') target =  .left.equalTo  target.parentElement

  make.top  console.log(target?.dataset.id)
}

Top rated topics

Using p4python with grep command

Object creation on Heap

Can't type in IntelliJ

Git refusing to merge unrelated histories on rebase

Exporting a class with Webpack and Babel not working

" top level design entity is undefined" ... what does it mean?

Automatically resize ggplot2 plots in flexdashboard

Singleton Class return new Instance every time

What happens behind the scenes(heap,stack,etc..) when adding a Node to the a LinkedList?

How to append tab on every new line in a custom pretty log format?

Groovy - Define variable where the variable name is passed by another variable

How can I specify the function type in my type hints?

How to load a PMML model?

'Invoke-Sqlcmd' is not recognized as the name of a cmdlet

Content inset not working Storyboard Xcode8

How to mask sensitive values in JSON for logging purposes

Java heap memory is larger than TOP

Include only part of font-awesome

Visual C++ 14 redist package prerequisite - configure from visual studio setup proj

Returning JSON data as a stream per chunk to Angular2 or jQuery over HTTP2 (HTTPS)

Laravel : How to store json format data in database?

ASCII art in C++

Hibernate could not get next sequence value

How to toast after pressing a big view notification button

MongoDB C# Get all documents from a list of IDs

Successful response from Super Stack API but account not charged

Use pytesseract OCR to recognize text from an image

Django nginx Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'

Python3.5 ImportError: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

How to build documentations project placed outside of docs directory using ReadTheDocs?

Using global css files with CSS Modules

Error:Uncaught TypeError: $(...).valid is not a function while using ('#form').valid()

List all bamboo variables in inline script

How to print British Pound Sign £ on printer in android code

Proper insertion of table name

How to replace nth char from a string in Go

Getting kernel version from the compressed kernel image

Instagram Scraping in PHP

Unity how to make a Visual JoyStick in Unity

React-native run-android is unrecognized

Migrating from TeamForge To CloudForge

How set column as date index?

Can not get IMSI and IMEI with AT commands

Indirect enums and structs

Mysql making --secure-file-priv option to NULL

"npm install [package]" doesn't update package.json

Redux: Why is avoiding mutations such a fundamental part of using it?

Operator precedence in c for "==" and "||"

What is Protocol Oriented Programming in Swift? What added value does it bring?

Email address is not verified (AWS SES)

Top