myPlacemark = new ymaps.Placemark([55.907228, 31.260503], { balloonContentHeader: "Балун метки", balloonContentBody: "<button class='set-moderated'>модерировать</button>", balloonContentFooter: "Подвал", hintContent: "Хинт метки" }); $('.set-moderated').on('click', function() { alert('успешно промодерировано'); }); 

I create a label with a balun, in balloonContentBody enter html with the class 'set-moderated' to trigger the jquery event, but it does not work. I know that there is a solution through the creation of your template. But is there a simpler solution? After all, I have many widgets on my site, and they all render in a standardized way where I need them - I’ll balloonContentBody them right there in balloonContentBody and render, and I don’t really like rewriting templates.

    2 answers 2

    Good day. At the moment of calling the "on" element, the ".set-moderated" is not yet on the page. In your case, the correct way is to delegate the event. Do not subscribe to the clicks of all ".set-moderated", and to the clicks of all ".set-moderated" inside some other element that already exists at the time of the call to "on".

    Something like

     $("#map").on( "click", ".set-moderated", function () { ... }); 

    Read more about event delegation in jquery http://api.jquery.com/on/#direct-and-delegated-events

      A very relevant topic for me ... tried to define onClick by passing it through properties in a custom template. Nothing happened - the function became attached to the string when passed to the template. Is it possible to associate a function with a click, without using the search by dom and global objects?

      • Thanks) I have already found the solution here) but I don’t know now how to run modal bootstrap which is also rendered in the code $ (document) .on ('click', '# AddmanualAddress1325' balun, function (e) {$ ("# manualAddress1325 "). modal ();}); code - Victor Vasiliev
      • It worked well for me when I hung up the handler when creating the balun: ymaps.templateLayoutFactory.createClass( `тут шаблон`, { build : function() { тут вешаю обработчик } }) - BOPOHOB