Error while using replace

0

I'm using the replace, but when I pass the number 1 or 12 to it the error. For example:

$(this).data('qtde').replace(/\./g, "")

Passing the value 1,223,44 works, when step 1 gives the following error:

  

Uncaught TypeError: $ (...).   HTMLTableRowElement. (Elaborate.js? V = 08022018: 792) at   Function.each (jquery-3.2.1.js: 362) at jQuery.fn.init.each   (jquery-3.2.1.js: 157) at HTMLInputElement.gravar   (Elaborate.js? V = 08022018: 782) at HTMLInputElement.dispatch   (jquery-3.2.1.js: 5206) at HTMLInputElement.elemData.handle   (jquery-3.2.1.js: 5014)

    
asked by anonymous 08.02.2018 / 19:58

2 answers

4

Try to make the number a string:

$(this).data('qtde').toString();
$(this).data('qtde').replace(/\./g, "");
    
08.02.2018 / 20:06
2

Dude, I have another solution that is to add a replaceAll function to variables of type string . I believe it to be your case, since it has a comma and period. As far as I understand, you want to give replace just in point. That is enough. See the example:

String.prototype.replaceAll = String.prototype.replaceAll || function(needle, replacement) {
    return this.split(needle).join(replacement);
};

var numbers = ['1.399,00', '1,00', '12,00', '1.399.244,00', '1', '12', '150.000,34'];
for (n in numbers){
  numbers[n] = numbers[n].replaceAll('.', '');
  console.log(numbers[n]);
  console.log("Normalizado: ", Number(numbers[n].replaceAll(',', '.')).toFixed(2) )
}
    
08.02.2018 / 20:06