I have seen the different solutions that store the info window in a variable and close it from outside of the info window, however I would like to have a possibility to close the info window from within itself, like having a button that closes the info window (in addition to the close button). I have seen that I can add the close class to an element, however this also adds the default styling, which I don't want.

Is there a good way to do this?

1 Answer 1


Check this example on how to close programmatically an Infowindow. You only need to attach that code to the proper event for your use case, being a keystroke, a button in your infowindow or outside, etc.

  • Thanks, but this is the example I referenced to above as well. How would you use this in a "clean" way, from inside the template? Currently, I put a method into the global window object to reference from the template, which is probably the worst solution. I have more than one sublayer, with infowindows which makes all that a bit more tricky as well.
    – Robin
    Commented Jun 27, 2017 at 18:31
  • Check this other example on templating infowindows. You can attach any logic to your infowindow. Hope this completes my answer.
    – Jorge Sanz
    Commented Jun 28, 2017 at 12:56
  • Hi Jorge, no I have custom templates and I know how to programmatically close an infowindow. The question is about, how to do it in the infowindow without ugly hacks. I need to put the infowindow reference into a global js variable to retrieve it from the infowindow. This works, but it is horrible code, so I was hoping for a good solution like writing in the infowindow this.setVisibility('false' ), which does not work BTW.
    – Robin
    Commented Jun 29, 2017 at 13:16
  • 1
    You have a place to run code when the infowindow is created. You don't have to attach it to a global variable, but that depends on parts where CartoDB.js is not having an opinion. If you are coding a Backbone app for example, you will have a place where you store your map model, you can save there a reference to your infowindow and then offer a service to change its visibility.
    – Jorge Sanz
    Commented Jun 30, 2017 at 14:28
  • Ah, you are right, I haven't thought about that. That would indeed work, thanks.
    – Robin
    Commented Jul 2, 2017 at 9:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.