I am making a carto map using carto.js and a map/data from my Carto account. I know I can set the interactivity there for clicking on polygons, etc. in the wizard, but am not able to alter that in my carto.js code.


cartodb.createLayer(map, vizjson)
                .on('done', function(layer) {

                    var subLayerOptions = {
                        sql: "SELECT * FROM final_1",
                        cartocss: "#final_1{polygon-fill: #F84F40; line-color: #000;}",
                        interactivity: "name"

                    var sublayer = layer.getSubLayer(0);



                    alert("this is a map");


My subLayerOptions interactivity line does not seem to have any effect on the data when I include that line. Instead, it just displays a popup that is forever "loading". How can I set the interactivity in the carto.js code instead of on the Carto builder side?

1 Answer 1


After setting the interactivity, you need to add an infowindow to display a pop up. Here you have the documentation. In addition, have a look at this working example. Basically, you have to add two things:

  • an infowindow template, such as this one:
<!-- HTML template for custom infowindow -->
<script type="infowindow/html" id="infowindow_template">
    <div class="infowindow-custom">
      <a href="#close" class="cartodb-popup-close-button close">x</a>
       <div class="cartodb-popup-content">
        <div class="content" style="padding:20px">
      <div class="cartodb-popup-tip-container"></div>
  • and add this method to the visualization object:

cdb.vis.Vis.addInfowindow( map, layer, ['name'], { infowindowTemplate: $('#infowindow_template').html() });

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.