inventory system - dynamically change input value

Asked

Viewed 35 times

-1

People would like the "status" field to change its value, if the value of the "quantity" field was less than 5.

I tried the code below but it doesn’t work. I don’t know where I’m going wrong

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        var x = document.getElementById("quantidade")
        function aviso(){
            if(x < 5){
                document.getElementById("status").value = 'Quantidade crítica. Comprar mais!!'
            }else{
                document.getElementById("status").value = 'Em estoque'
            }
        }
    </script>
    <title>teste</title>
</head>
<body>
    Quantidade: <input type="text"  id="quantidade" onkeyup="aviso()"><br><br>
    status: <input type="text"  id="status"><br><br>                    
</body>
</html>

1 answer

0

You gotta get the value of the element x every call of the function aviso.

<!DOCTYPE html>
<html>
<head>
    <title>teste</title>
    <meta charset="UTF-8">
    <script type="text/javascript">
        function aviso(){
            let x = document.getElementById("quantidade").value
            console.log(x)
            if(x < 5) {
                document.getElementById("status").value = 'Quantidade crítica. Comprar mais!!'
            } else {
                document.getElementById("status").value = 'Em estoque'
            }
        }
    </script>
</head>
<body>
    Quantidade: <input type="text"  id="quantidade" onkeyup="aviso()"><br><br>
    status: <input type="text"  id="status"><br><br>
</body>
</html>

In addition, I added the tag meta to correctly display special characters such as í.

  • thanks @chriptus13, it only works when I compare x to a number. when I compare it to a y variable, it only works in iteration. knows how to solve this?

  • Don’t forget to mark as correct.

Browser other questions tagged

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