Quote an array - Jquery

Asked

Viewed 36 times

-1

I need to put inside each element of my array, simple quotes.

In the code below, I check which checkbox is selected, and insert in my array.

var checkbox = $('input:checkbox[name^=checkregiao]:checked');
            //verifica se existem checkbox selecionados
            if(checkbox.length > 0){
                //array para armazenar os valores
                var regioes_selecionadas = [];
                //função each para pegar os selecionados
                checkbox.each(function(){
                    regioes_selecionadas.push($(this).val());
                });
               alert(regioes_selecionadas);
             }

My Alert displays: "SP,MG,RS,BA" in this way.

I need it to look like this: "'SP','MG','RS','BA'". Look each element is separated by simple quotes..

I was able to use the command: var regioes_selected = JSON.stringify(regioes_selected);

It’s got double quotes. "SP","MG","RS","BA"

I need simple quotes "'SP','MG','RS','BA'" I need simple quotes. Before posting here, I search, I try to find a solution.

1 answer

1

If you want to print a string in a specific format, generate a string, not an array that will be printed in different ways depending on the context.

You can start by adding the quotes in the checkbox values themselves

regioes_selecionadas.push("'" + $(this).val() + "'");

Then generate the string, separating the values by comma using the method join:

var regioes_str = regioes_selecionadas.join(",");
alert(regioes_str);
  • Thank you so much for your great help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.