0

I add custom button to map. This button display on left top position of screen.

JSFIDDLE demo here.

I need attach data to button. In my code I add a number to the button. After clicking I would like to display this number in console. Please help me.

JS:

ymaps.ready(init);

var map;

function init(){     
    map = new ymaps.Map("map", {
        center: [12.12, 12.12],
        controls: [],
        zoom: 5
    });  

    var btn = new ymaps.control.Button({
        data: {
            content: "bla",
            num: 77783
        }
    });

    map.controls.add(btn, {
        float: 'left',
    }); 

    btn.events.add('click', (e) => {
        console.log('binded data is:', e.get('num'))
    });    
};
ptortyr45
  • 147
  • 1
  • 11

1 Answers1

1

You need to change this function:

btn.events.add('click', (e) => {
    console.log('bound data is:', e.get('num'))
});   

to:

btn.events.add('click', (e) => {
    console.log('bound data is:', e.originalEvent.target.data.get('num'))
});

The updated fiddle

c-smile
  • 26,734
  • 7
  • 59
  • 86
gaetanoM
  • 41,594
  • 6
  • 42
  • 61