Get element from which onclick function is called

Black

I have a button with a onclick attribute which is pointing to the function test().

<button onclick="test()">Button 1</button>
<button onclick="test()">Button 2</button>
<button onclick="test()">Button 3</button>

Function test():

function test()
{
    var button_name = this.html;
    console.log("Im button "+ button_name);
}

How can I get informations about the clicked button? e.g. How can i read the html?

jsfiddle: https://jsfiddle.net/c2sc9j9e/

Satpal

Pass the this reference to the function, then read textContent property the text content of the node.

HTML

<button onclick="test(this)">Button 1</button>

Script

function test(elem){
   var button_name = elem.textContent;
}

Fiddle

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

How to know from which perl module or file the function was called

分類Dev

How to return which element from the tuple matched in the endswith function

分類Dev

How to determine which button called a function?

分類Dev

How to get which element was clicked?

分類Dev

which value does javascript function takes if the function is called in the given example?

分類Dev

Not able to get value from Async function which returns value by calling asyncStorage function

分類Dev

How to get all element from div block which contains type=radio?

分類Dev

Get value from input with onclick

分類Dev

onClick on Cardview get from firebase

分類Dev

Update state with value from button element onClick

分類Dev

how to get random element of array which is not null?

分類Dev

Get the element (node) which triggered an event

分類Dev

HttpPost function in controller doesn't get called

分類Dev

Xamarin forms DisplayAlert not showing when called from a function called by a delegate

分類Dev

Reloading UITableViewController from outside a function called in viewDidLoad

分類Dev

JavaScript dynamically element duplicate each time function is called

分類Dev

exit/break main function from called/child function

分類Dev

Swift: Get an element from a tuple

分類Dev

Get struct from function

分類Dev

Is there a variant of min_element which takes a projection function?

分類Dev

Asyncio function works when called from script but not from Flask route

分類Dev

Return value from setInterval() function called from html

分類Dev

How to prepend html into on page element from HTML onClick event?

分類Dev

get an element from array with http GET php

分類Dev

Get an element of a String, which looks like XML and manipulate it in java

分類Dev

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

分類Dev

Global function declared in background page does not get called via evaluateHandle

分類Dev

Share Facebook or Line with onclick function, but get SEO problem "Links are not crawlable"

分類Dev

Is there any other less processor intensive way to limit the speed at which a function is called other than busy-waiting?

Related 関連記事

  1. 1

    How to know from which perl module or file the function was called

  2. 2

    How to return which element from the tuple matched in the endswith function

  3. 3

    How to determine which button called a function?

  4. 4

    How to get which element was clicked?

  5. 5

    which value does javascript function takes if the function is called in the given example?

  6. 6

    Not able to get value from Async function which returns value by calling asyncStorage function

  7. 7

    How to get all element from div block which contains type=radio?

  8. 8

    Get value from input with onclick

  9. 9

    onClick on Cardview get from firebase

  10. 10

    Update state with value from button element onClick

  11. 11

    how to get random element of array which is not null?

  12. 12

    Get the element (node) which triggered an event

  13. 13

    HttpPost function in controller doesn't get called

  14. 14

    Xamarin forms DisplayAlert not showing when called from a function called by a delegate

  15. 15

    Reloading UITableViewController from outside a function called in viewDidLoad

  16. 16

    JavaScript dynamically element duplicate each time function is called

  17. 17

    exit/break main function from called/child function

  18. 18

    Swift: Get an element from a tuple

  19. 19

    Get struct from function

  20. 20

    Is there a variant of min_element which takes a projection function?

  21. 21

    Asyncio function works when called from script but not from Flask route

  22. 22

    Return value from setInterval() function called from html

  23. 23

    How to prepend html into on page element from HTML onClick event?

  24. 24

    get an element from array with http GET php

  25. 25

    Get an element of a String, which looks like XML and manipulate it in java

  26. 26

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

  27. 27

    Global function declared in background page does not get called via evaluateHandle

  28. 28

    Share Facebook or Line with onclick function, but get SEO problem "Links are not crawlable"

  29. 29

    Is there any other less processor intensive way to limit the speed at which a function is called other than busy-waiting?

ホットタグ

アーカイブ