Split "100000000000" in JQUERY [closed]

1

I need to separate all the characters of a string into an array, and I have a problem, my code is as follows

var PISCampos = campos.split(""); 

When I send in the variable fields the following string: "011001000000". Everything works, and I have tested with others like "000001000000" and the split works perfectly

But using this string: "100000000000" split does not work. I put an alert on the top line and one on the bottom line, only the first one works with this string, suggesting that the error is right there. The first alert displays the variable fields, and its value is correct.

TEST CODE

alert(campos);
var PISCampos = campos.split("");
alert("teste");

THE PROBLEM

I used this to retrieve the value I told them ("011001000000")

$("#campo option:selected" ).data("campos")

And my HTML was like this

    <option data-campos="100000000000" value="53">Bla Bla Bla</option>

But from what I realized, since the first character was a nonzero number, jquery val () called it an integer, my fault, my sincere apologies.

SOLUTION

campos = campos.toString();
var PISCampos = campos.split("");
    
asked by anonymous 20.10.2016 / 17:12

1 answer

3

It was supposed to work, split is a function of the String class.

That works :

var campos = "100000000000";
var PISCampos = campos.split("");

PISCampos.forEach(function(e){alert(e)}); // Somente para mostrar

That does not work :

var campos = 100000000000;
var PISCampos = campos.split("");

PISCampos.forEach(function(e){alert(e)}); // Somente para mostrar

Verify that the variable you are passing is a String, and try using a console to see if you are printing a Javascript error (Chrome dev console or Firebug for Mozilla).

JSFiddle working

    
20.10.2016 / 17:21