After ordering the code to return an Alert if the input values are reset, when pressing the button the page hangs

Asked

Viewed 51 times

-3

When the values of the inputs are zeroed, the right one was to appear a alert and a change in the div, but the page hangs and I can no longer put numbers or click the button.

I realized that when I take the .lenght the code works.

Why the lenght hangs the page?

function tabuada(){
let ini=document.getElementById('txt1')
let fim=document.getElementById('txt2')
let passo=document.getElementById('txt3')
let res=document.getElementById('res')

if (ini.value.lenght== 0 || fim.value.lenght== 0 || passo.value.lenght== 0){
    window.alert('[ERRO], dados faltando...')
    res.innerHTML='Impossível de contar...'
}
else{
    res.innerHTML='Contando:'
    let i=Number(ini.value)
    let f=Number(fim.value)
    let p=Number(passo.value)
if(i<f){
    //Contagem Crescente
    for(let c=i; c<=f; c += p){
        res.innerHTML+=`${c}\u{1F449}`
    }
        res.innerHTML+=`\u{1F3C1}`
}
else{
    //Contagem Regressiva
    for(let c=i; c>=f; c-=p){
        res.innerHTML+=`${c}\u{1F449}`
    }
        res.innerHTML+=`\u{1F3C1}`
}
}
}
  • 2

    The lenght is wrong. Right is right length. Try it like this: if (!+ini.value.trim() || !+fim.value.trim() || !+passo.value.trim()){

  • Samzon present !

  • @Sam Whereas an empty string is considered false, nor needed to use the + to convert to number: if (!ini.value.trim() || !fim.value.trim() || !passo.value.trim()). By the way, converting to number can be a problem, because the string "000" is converted to the zero number (that is, even if it is not empty, it will enter the if).

  • Unless, of course, he also wants to validate that the value is zero (which was not clear in the question). Anyway...

  • @hkotsubo That’s right. He wants to check whether it is empty or zero to fire the Alert.

1 answer

0

In the if you use ini.value.length. This is not possible for inputs as inputs do not have length. The elements are the ones you assigned in the variables in document.getElementByID, that cannot be used in the way you reported in the question, IN THIS CASE.

In your case, as you said you are using inputs, then you must use value.

You mentioned that you want to check if the field is 'zeroed'. I don’t understand if you mean if the field has 0 (zero) or if the field is empty ("").

I will make an example with the two options for you to observe.

  • If your case is if the field has 0 (zero):

if (ini.value == 0 || fim.value == 0 || passo.value == 0) {
  //Sua condição aqui
}

  • Now if your case is if the field is empty (""):

if (ini.value == "" || fim.value == "" || passo.value == "") {
  //Sua condição aqui
}

  • 1

    It is perfectly possible to use ini.value.length, for the value is a string and strings have length. The problem was the typo, because the question was used lenght ("ht" instead of "th"). E input does not have length, only minlength and maxlength, see.

  • Okay, I corrected the answer. Thank you, man. I read your link.

Browser other questions tagged

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