Hi,
I’ve been trying to modify the below code.
$w.onReady(() => {
$w('#input1').onChange( event => {
$w('#input1').value = parseFloat('number').toFixed(0)
})
})
What I want basically is when a user inputs a number I would like the number to automatically round off to positive integer. As for an example, if the user types in $345.90, i want to automatically generate to a $346 without having to click an additional button.
Thank you.
This is really a JS question not a Wix Corvid question, so you would be better off looking for this answer somewhere like w3schools or stackoverflow, e.g. https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary or https://www.w3schools.com/jsref/jsref_round.asp
But to answer your question - I would use:
$w('#input1').value = Math.round(number);
hi,
I tried but unable to get it right. Not sure what I’m doing wrong. I want the input to change immediately once the user entered the number.
Can help me to check this out?
$w.onReady(() => {
$w('#input1').onChange( event => {
$w('#input1').value = Math.round(number);
}) ;
}) ;
J.D
November 25, 2019, 7:22pm
4
@testsalesmimota you need to use onKeyPress instead and to add a short timeout.
Something like:
$w.onReady(() => {
$w("#input1").onKeyPress(event => {
setTimeout(() => {
if (!isNaN($w("#input1").value)){
$w("#input1").value = Math.round(Number($w("#input1").value ));
}
}, 20)
})
})