Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  23] [ 6]  / answers: 1 / hits: 17548  / 8 Years ago, tue, october 25, 2016, 12:00:00

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/


More From » jquery

 Answers
16

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(clickedElement){
var button_name = clickedElement.textContent;
}

Fiddle


[#60281] Sunday, October 23, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
chyanne

Total Points: 208
Total Questions: 120
Total Answers: 110

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;