K
K
Kerm2019-09-03 10:46:12
API
Kerm, 2019-09-03 10:46:12

How to add an event to ymaps.control.ListBoxItem when a menu item is selected?

new ymaps.control.ListBoxItem({
      data: {
        content: 'Владивосток',
        center: [43.115536, 131.885485],
        zoom: 9
      }
    })

How can I add an onclick event inside the code with the menu item declaration that will trigger my function?
I tried like this:
new ymaps.control.ListBoxItem({
      data: {
        content: 'Екатеринбург',
        center: [56.829103, 60.598939],
        zoom: 16
      },
      events: {
        click: function() {
          alert(1);
        }
      }
    }),

But you see, I'm doing something wrong, I did not find an example in Google.
This doesn’t suit me, I need to run the function exactly when I click on the menu item with its own passed parameter for each item:
myPlacemark.events.add('click', function () {
    alert('О, событие!');
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2019-09-03
@deepblack

Code (spoiler)
const cityList = new ymaps.control.ListBox({
    data: {
        content: 'Select a city'
    },
    items: [
      new ymaps.control.ListBoxItem('Moscow'), 
      new ymaps.control.ListBoxItem('Novosibirsk'), 
      new ymaps.control.ListBoxItem({
        options: {
          type: 'separator'
        }
      }), 
      new ymaps.control.ListBoxItem('New York'), ]
  });
  cityList.get(0)
    .events.add('click', function () {
    myMap.setCenter([55.752736, 37.606815]);
  });
  cityList.get(1)
    .events.add('click', function () {
    myMap.setCenter([55.026366, 82.907803]);
  });
  cityList.get(3)
    .events.add('click', function () {
    myMap.setCenter([40.695537, -73.97552]);
  });
  myMap.controls.add(cityList, {
    floatIndex: 0
  });

Press Select a city, choose a city from the list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question