D
D
Denis Artemiev2015-10-26 11:00:11
JavaScript
Denis Artemiev, 2015-10-26 11:00:11

How to call a function on onclick?

Hello. I have the following problem, it is probably very stupid)

I have a ymaps function in which the myMap variable is defined. In this function, a map is built, and also displayed in #table lines from json.

The select_street function is defined below. In which there is a call to myMap, which, of course, does not work. If you make an alert in this function, then the parameters are accepted and displayed.

The problem is that if we move the select_street function to ymaps. Then the select_street function does not work at all, even alert.

PS: the group.items[j].center variable contains the coordinates: "53.6562323,52.565623232". Therefore, the select_street function takes two parameters.

ymaps.ready(function () {
    var myMap = new ymaps.Map('map', {
            center: [42.31599819, 69.59457850],
            zoom: 9,
            controls: ['zoomControl', 'typeSelector', 'fullscreenControl', "trafficControl", "geolocationControl"]
        });

//тут еще много кода, tr генерируется в цикле

 var submenuItem = $('<tr onclick="select_street('+coutn+','+group.items[j].center+')"><td>' + group.items[j].name + '</td><td>' + group.items[j].p + '</td><td>' + group.items[j].pr + '</td><td></td><td>' + group.items[j].p + '</td><td>' + group.items[j].pr + '</td><td><span class="tel"></span></td></tr>');
            submenuItem.appendTo($('#table'));

//этот обработчик на левой кнопке срабатывает, может в него можно передать параметры?
$("#b_calc").click(function () {
    myMap.setCenter([42.31599819, 69.59457850], 13, "map");
    myMap.panTo([[42.31599819, 69.59457850]);
    geoObjects[2].balloon.open();
});

});
function select_street(count, coordinates1, coordinates2)
{
    myMap.setCenter([coordinates1,coordinates2], 13, "map");
// Центрирование карты на добавленном объекте
    myMap.panTo([coordinates1,coordinates2]);
    geoObjects[count].balloon.open();
}


Thanks in advance for your advice)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
N
nirvimel, 2015-10-26
@ArtDenis

You can enter a variable from above (outside all handlers):
Then simply replace:
with
myMap = new ymaps.Map()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question