How to search an Event to get the name of an Element?

CodeMan03

Lets say I have a clipboard paste event inside my component like below.

window.addEventListener('paste', this.insertNewRowsBeforePaste.bind(this));

Then when I hit the ctrl + V on my keyboard it calls this event which is a ClipboardEvent

insertNewRowsBeforePaste(event) {   
const tabName = // <---- I need to be able to see which tab the user has clicked on then I can do other processing based on the current tab.
}

So I'm expecting it to say tabName = 'Tab 1' or 'Tab 2' etc since my component is a tab control with multiple tabs.

Alexander Staroselsky

EventTarget event.target will provide the HTMLElement that triggered the event. You can then target the name property on elements such as <button>, <input>, and <a>. You also have access to additional properties such as innerHTML and textContent to check against.

insertNewRowsBeforePaste(event) {   
    const target = event.target;
    const name = target.name; 

    console.log(name);

    if (name.indexOf('Tab 1') > -1) { console.log('Tab 1'); }
}

Here is an example using event.target of a clicked elements to access their respective name, innerHTML and textContent property values.

If you are doing this in TypeScript, you will need to use casting/type-assertion on the target to access HTMLButtonElement | HTMLInputElement | HTMLAnchorElement properties such as name:

insertNewRowsBeforePaste(event) {
    const target = event.target as HTMLButtonElement;
    const name = target.name;

    // or
    // const name = (<HTMLButtonElement> event.target).name;

    // or
    // const target = event.target as HTMLButtonElement;
    // const { name } = target;        
}

Hopefully that helps!

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to get the child of a element with event.target

分類Dev

How to get the element that received the keydown event in TINYMCE

分類Dev

How to get $scope or $element in an onblur event and run a function in an onblur event?

分類Dev

Python tinydb - How to get specific element name?

分類Dev

How to get Element Properties in React Native on a Click Event

分類Dev

How to search element in a List?

分類Dev

How to search an element in xml?

分類Dev

How to get attribute's element name in the XML/XSD

分類Dev

How to get element by attribute name which ends with defined word

分類Dev

How to get the table-name and database-name in the CDC event received from debezium kafka connect

分類Dev

How to search for an element in a golang slice

分類Dev

Get the element (node) which triggered an event

分類Dev

Fire click event for only one element with same class name

分類Dev

How do I get the name from object and assign it to a new created element?

分類Dev

Get Class Name of Element from MouseEvent Target

分類Dev

How to get grouped event frequency?

分類Dev

How to get module name by file name?

分類Dev

Vue.js + Element UI: Get "event.target" at change

分類Dev

How to add an attribute with a hyphen in the name to a script element?

分類Dev

How to set name and id on a TR element?

分類Dev

How to replace name attribute inside of element in JQuery?

分類Dev

How to find a value from a specific element by name?

分類Dev

How to print the element name when using axes?

分類Dev

How to get property of struct by name?

分類Dev

How to get a font PostScript name?

分類Dev

How to get program name in perl

分類Dev

How to get MTP device name

分類Dev

How to get the last element of a slice?

分類Dev

How to get an attribute of an Element that is namespaced

Related 関連記事

  1. 1

    How to get the child of a element with event.target

  2. 2

    How to get the element that received the keydown event in TINYMCE

  3. 3

    How to get $scope or $element in an onblur event and run a function in an onblur event?

  4. 4

    Python tinydb - How to get specific element name?

  5. 5

    How to get Element Properties in React Native on a Click Event

  6. 6

    How to search element in a List?

  7. 7

    How to search an element in xml?

  8. 8

    How to get attribute's element name in the XML/XSD

  9. 9

    How to get element by attribute name which ends with defined word

  10. 10

    How to get the table-name and database-name in the CDC event received from debezium kafka connect

  11. 11

    How to search for an element in a golang slice

  12. 12

    Get the element (node) which triggered an event

  13. 13

    Fire click event for only one element with same class name

  14. 14

    How do I get the name from object and assign it to a new created element?

  15. 15

    Get Class Name of Element from MouseEvent Target

  16. 16

    How to get grouped event frequency?

  17. 17

    How to get module name by file name?

  18. 18

    Vue.js + Element UI: Get "event.target" at change

  19. 19

    How to add an attribute with a hyphen in the name to a script element?

  20. 20

    How to set name and id on a TR element?

  21. 21

    How to replace name attribute inside of element in JQuery?

  22. 22

    How to find a value from a specific element by name?

  23. 23

    How to print the element name when using axes?

  24. 24

    How to get property of struct by name?

  25. 25

    How to get a font PostScript name?

  26. 26

    How to get program name in perl

  27. 27

    How to get MTP device name

  28. 28

    How to get the last element of a slice?

  29. 29

    How to get an attribute of an Element that is namespaced

ホットタグ

アーカイブ