Error when using replace

Asked

Viewed 345 times

0

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

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

Passing the value 1.223.44 works, when step 1 of the following error:

Uncaught Typeerror: $(...). data(...). replace is not a Function at 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.write (Elaborar.js?v=08022018:782) at Htmlinputelement.Dispatch (jquery-3.2.1.js:5206) at Htmlinputelement.elemData.Handle (jquery-3.2.1.js:5014)

  • Which error displayed?

  • Uncaught Typeerror: $(...). data(...). replace is not a Function at Htmltablerowelement.<Anonymous> (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.write (Elaborate.js? v=08022018:782) at Htmlinputelement.Dispatch (jquery-3.2.1.js:5206) at Htmlinputelement.elemData.Handle (jquery-3.2.1.js:5014)

2 answers

4


Try to make the number a string:

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

2

Dude, I have another solution that is to add a function replaceAll in variables of the type string. I believe it is your case, since you have comma and period. From what I understand, you want to give replace only at the point. That’s 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) )
}

  • So William, my problem is that sometimes the user can enter 1 and can also enter 1.23...

  • I edited. I put several possibilities, including the 1. You can do with any number that has point and comma, it will remove all points.

Browser other questions tagged

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