Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-1
rated 0 times [  0] [ 1]  / answers: 1 / hits: 56678  / 12 Years ago, mon, september 17, 2012, 12:00:00

For example I want a function that is used by many elements to get the attributes of the calling element.



function example(){
var name = //name of the calling element $(this).attr('name')
}
<button name=somename1 onclick=example()>Button1</button>
<button name=somename2 onclick=example()>Button2</button>


so if the button named 'somename1' calls the function, the variable 'name' will be assigned to 'somename1' and so if 'somename2' called it, it will be assigned to 'somename2'


More From » jquery

 Answers
8

Use This:



function exampleFunction(exampleElement) {
var name = exampleElement.name;
}

<button name=somename1 onclick=exampleFunction(this)>Button1</button>
<button name=somename2 onclick=exampleFunction(this)>Button2</button>


But if you use jquery, you could do



$('button').click(function() {
var name = $(this).attr('name');
});


Without the onclick attribute.


[#83053] Friday, September 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
christianu

Total Points: 481
Total Questions: 124
Total Answers: 99

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
2 Years ago
christianu questions
;