Is it possible to dynamically acquire the name of the element that calls a function with the onClick event?
I need to write a code to reuse massively in a form to validate the content.
Is it possible to dynamically acquire the name of the element that calls a function with the onClick event?
I need to write a code to reuse massively in a form to validate the content.
It's a little tough to say this is exactly what you are looking for, but something like this?
onClickHandler(event) {
var domNode = event.targetElement;
var name = domNode.getAttribute("name");
// do what you will with that
}
Getting the element this way makes sure it was what you clicked on - using this
can be different depending on if you bound scope to the method along the way somewhere
Let me answer with an example if this is what you are looking for:
Html:
<input name='Foo" type='button' value='Click Me' onclick='bar(this)'>
JS:
function bar(elem) {
alert('Clicked by' + elem.name);
}
You can use parentNode to navigate in your form.
Yes you can do this by passing an event to the respective onClick function as stated below.
<a href="#" id="testLink" onclick="HandleLink(event)">Link</a>
function HandleLink(event)
{
var x = event.target;
alert(x.id);
}