admin管理员组文章数量:1023025
I'm trying to make my IMask
input update its value to be cleared when it is clicked but IMask
is throwing a warning:
Element value has changed outside the mask. Sync the mask using mask.updateValue() for it to work correctly.
I've tried to use all of the ways I could find to update the value, such as mask.updateValue()
though I still see the error.
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number");
const mask = IMask(input, {
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
I'm trying to make my IMask
input update its value to be cleared when it is clicked but IMask
is throwing a warning:
Element value has changed outside the mask. Sync the mask using mask.updateValue() for it to work correctly.
I've tried to use all of the ways I could find to update the value, such as mask.updateValue()
though I still see the error.
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number");
const mask = IMask(input, {
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
Share
Improve this question
edited Jun 17, 2023 at 0:42
Skully
3,1263 gold badges27 silver badges41 bronze badges
asked Feb 25, 2023 at 0:23
RafaelRafael
131 silver badge5 bronze badges
2 Answers
Reset to default 2You are seeing the warning regarding the value being out of sync because you are updating the input field's value directly on the DOM element rather than using the IMask
object, this updates the input field however IMask
cannot see this change.
The correct way to update the value of an IMask
input is to directly update the value of the .value
property of the IMask
object you created:
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number"); // This is your raw DOM element.
const mask = IMask(input, { // This is your IMask object.
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
mask.value = ""; // Clear IMask object value.
});
See 'Get/set value and unmasked value' here: https://imask.js/guide.html#masked-base
In your case, you should just write it like this:
input.addEventListener("click", () => {
mask.updateValue("");
});
instead of
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
I'm trying to make my IMask
input update its value to be cleared when it is clicked but IMask
is throwing a warning:
Element value has changed outside the mask. Sync the mask using mask.updateValue() for it to work correctly.
I've tried to use all of the ways I could find to update the value, such as mask.updateValue()
though I still see the error.
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number");
const mask = IMask(input, {
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
I'm trying to make my IMask
input update its value to be cleared when it is clicked but IMask
is throwing a warning:
Element value has changed outside the mask. Sync the mask using mask.updateValue() for it to work correctly.
I've tried to use all of the ways I could find to update the value, such as mask.updateValue()
though I still see the error.
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number");
const mask = IMask(input, {
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
Share
Improve this question
edited Jun 17, 2023 at 0:42
Skully
3,1263 gold badges27 silver badges41 bronze badges
asked Feb 25, 2023 at 0:23
RafaelRafael
131 silver badge5 bronze badges
2 Answers
Reset to default 2You are seeing the warning regarding the value being out of sync because you are updating the input field's value directly on the DOM element rather than using the IMask
object, this updates the input field however IMask
cannot see this change.
The correct way to update the value of an IMask
input is to directly update the value of the .value
property of the IMask
object you created:
import IMask from "imask";
import {contador} from "./lib/contador";
import {NewGame} from "./lib/novoJogo";
import {SorteioNumero} from "./lib/sorteioDoNumero";
let input = document.querySelector("#number"); // This is your raw DOM element.
const mask = IMask(input, { // This is your IMask object.
mask: Number,
min: 0,
max: 10,
});
input.addEventListener("click", () => {
mask.value = ""; // Clear IMask object value.
});
See 'Get/set value and unmasked value' here: https://imask.js/guide.html#masked-base
In your case, you should just write it like this:
input.addEventListener("click", () => {
mask.updateValue("");
});
instead of
input.addEventListener("click", () => {
input.value = "";
mask.updateValue();
});
本文标签: htmlJavaScript How to update the value of IMask elementStack Overflow
版权声明:本文标题:html - JavaScript How to update the value of IMask element - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745562068a2156220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论