I am trying to change the color of my map layer when filter buttons are selected. So, the goal is: the user sees all of the data in one color. Then, when they click on a filter button, only a subset of the data is shown and in a different color.

I am looking for a command to call the previously defined styles when buttons are selected.

I have several map styles set up like this, each with a different polygon-fill (bluestyle, redstyle, etc):

<style type="cartocss/html" id="bluestyle">
          polygon-fill: #11799F;
          polygon-opacity: 0.7;
          line-color: #FFF;
          line-width: 0.5;
          line-opacity: 1;


Then, the code below (much of which is based on CartoDB's interactivity tutorial). Note I added "**" around the spot where I need to plug in this style change.

  function createSelector(layer) {
            var condition = "";
            var $options = $(".layer_selector").find("li");
            $options.click(function(e) {
                var $li = $(e.target);
                var selected = $li.attr('data');
                var type = $li.data('type');
                if (type === "cartocss") {
                    if (selected !== "bluestyle") {
                    condition = $('#'+selected).text();
                }  else {
                    if (selected !== "") {
                    if (selected.indexOf('Button1') !== -1) {
                        **(some code that will change the style to redstyle)** ;
                    } else {

                    layer.setSQL("SELECT * FROM " + tableName + selected);

I am new to Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.