[Solved] How to get the name of a clicked button


#1

Hi,

Here is my case, I generate father elements with sons. Each father element have a button to add a son. So I putted the id of the father in the name of my button :

<button class="btn btn-primary" name="{{_id}}" id="addme">add me</button>

Now i’m trying to put in variable the the name of this button :

var buttonName = event.target.addme.name; 

But it seems that it is not the way, I’m thetting this error :

Uncaught TypeError: Cannot read property ‘name’ of undefined.

I thanks anyone of you in advance for your help.


#2

console.log(event.target) and find it in console browser
I would expect it to be available also by something like event.target.prop(‘name’)


#3

Hi thanks for the reply.

It helped me a lot, and now with :

var buttonName = event.target.name;

it works like a charm !

Thanks a lot


#4

Its important to remember that helpers and events are called with the context being the item rendered.

There’s no need to store the id in the name field.

Inside your click handler you should just be able to do: this._id.

Provided of course it’s registered as a meteor template event.

Here’s some more info on this:


https://www.discovermeteor.com/blog/a-guide-to-meteor-templates-data-contexts/