1

I am working with the Google Maps API and multiple markers. On a marker, there needs to be a click event to open an info window. I got that part working. I was wondering if there was a way to pass multiple variables and add the same event listener on all those markers?

I currently have:

google.maps.event.addListener(marker, 'click', function () {
                        _(that.popups).each(function (popup) {
                            popup.hide();
                        }).value();

                        infowindow.show(/*that.map, marker*/);
                    });

If I have a nyMarker variable, then a laMarker variable, how do I pass on this to the event handler?

0

2 Answers 2

2

Its very simple; there are two methods as you want to do, both of them work; what you have to do is just use forEach() like this -

#First Method

  1. Here first you have to add a new and same class to all your elements (or use the old one if you already have) and then access them using querySelectorAll() function.

  2. Then you have to use forEach() to target all of them

// accessing the elements with same classname using querySelectorAll() function
const elements = document.querySelectorAll('.my-box');

// adding the event listener by looping
elements.forEach(element => {
   element.addEventListner('click', ()=>{
     console.log('action');
   });
});

#SecondMethod

  1. First you have to initialize all the variables through querySelector() or getElementsByClassName()[] or how ever you want do so..

const element1 = document.querySelector('.a');
const element2 = document.querySelector('.b');
const element3 = document.querySelector('.c');

  1. Then you have to target them using forEach() like this -

[element1, element2, element3].forEach((element)=>{
    element.addEventListner('click', ()=>{
      console.log('action');
   });
});

Sign up to request clarification or add additional context in comments.

Comments

0

This is just possible by adding them to one result-set. I would prefer using jQuery instead of plain javascript:

var elems = [$element1, $element2, $element3, ...];
var $result = $();

$.each(elems, function() {
    $result = $result.add(this);
});

$result.bind(...);

Multiple values are not working in this case :)

Hope this helps! Let me know if you got further questions!

5 Comments

I saw the answer for elements but my markers are not elements. they are variables that have data. I am not sure if this is the same logic.
Looks like I could't follow your thoughts the right way... What do you mean with I was wondering if there was a way to pass multiple variables to an event listener because i have multiple different marker variables.? :) @user3730179
my variable is not an element. an example is this jsfiddle.net/drmak3os/1
>So then I'm pretty sure yo are able to use the variables if they are defined before the event listener. Don't you? @user3730179
@user3730179 so I can keep adding variables after the comma? so google.maps.event.addListener(marker, laMarker, nyMarker ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.