0

In meteor I try to catch click event on a button.

I have :

events = { 'click .plat': function(event, template)
            {   
                console.log("event.target");
                console.log(event.target);
                id          = event.target['data-target'].value;
            }
         }

But I got an error when executed :

enter image description here

What seems strange to me is that the target is well logged into the console, and we see the "data-target" attribute.

Why can't I access it through [event.target['data-target']' ?

EDIT

Trying :

events = { 'click .plat': function(event, template)
            {   
                console.log("event.target");
                console.log(event.target);
                console.log(event.target.data);
                id          = event.target.data('target');
                console.log(id);

            }
         }

Getting :

enter image description here

Romain Jouin
  • 4,448
  • 3
  • 49
  • 79

2 Answers2

0

Because that's not how HTML attributes translate into DOM node properties.

Since your attribute is a data attribute you can access it using $(event.target).data('target'). Also see How to get the data-id attribute?.

Community
  • 1
  • 1
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • 1
    sorry, there was an error in my answer. This should work now, but Felipeptcho's answer is probably the better solution anyways. – Christian Fritz Oct 19 '16 at 15:31
0

Use event.target.dataset.target.

See: https://developer.mozilla.org/en/docs/Web/Guide/HTML/Using_data_attributes

felipeptcho
  • 1,377
  • 2
  • 9
  • 23