I have an HTML input element and after trying to change the value of the user, try to force a decimal place in the input I am doing So, let's assume that the user enters "4", I run this code:
this.value = this.value.toFixed (1) But I get a JavaScript error in which "Object 4 does not have any fixed fixes."
It seems JavaScript is trying to process a verb as a number, but, er, why? And how can I avoid it?
this.value is a string You get it from an input element before you can use the numbering methods on it, it needs to be placed on the number: this.value = number (this. Value) .toFixed (1); Alternatively, you can use the unary + operator to put the string on the number: this. Value (+ this .value) .toFixed (1); If you need to remove the string suffix, you can use parseFloat : this.value = ParseFloat (this. Value) .toFixed (1);
However, to handle the hexadecimal format when casting number parseFloat not Should keep in mind> : this.value = + '0xF'; // 15 this.value = parseFloat ('0xF'); // 0
Comments
Post a Comment