How to add a click event to the Yandex Maps balloon button?

  • 0
    Hello, I'm trying to hang an on click event on the balloon button,
    'balloonContentBody': '<input type="button" value="Выбрать" class="baloon-button">'

    $.ajax({
            // получаю метки и балуны
            }).done(function(data){
                data = JSON.parse(data);
                if (data['success'] === true){
                    let objectManager = new ymaps.ObjectManager({
                        clusterize: true,
                        gridSize: 32,
                        clusterDisableClickZoom: true
                    });
                    map.geoObjects.add(objectManager);
                    objectManager.add(data['information']);
    
                    let layout = map.getBalloon().getLayout();
                    YMaps.jQuery(layout.getRootNodes()).find('.baloon-button').click(function() {
                        console.log('Do something...');
                    });
                } else{
                    console.log(data['error']);
                }
            });
    but I get an error
    map.getBalloon is not a function

    This option doesn't work either.
    $('.baloon-button').on('click', function(){ 
        console.log('Do something...'); 
    });
    JavaScript Anonymous, Oct 3, 2019

  • 1 Answers
  • 0
    Eugene replied in a comment

    via custom balloon template


    Something like this

    let myBalloonFooterBodyLayout = ymaps.templateLayoutFactory.createClass(
    '<input type="button" value="Выбрать" class="baloon-button">', {
    build: function() {
    myBalloonContentBodyLayout.superclass.build.call(this);
    $('.baloon-button').on('click', function() {
    console.log('Do something...');
    });
    }
    });
    let objectManager = new ymaps.ObjectManager({
    contentFooterLayout: myBalloonFooterBodyLayout
    });
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!