Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Javascript, how can I make input boxes that changes as the values inside them change?

I know I might not be explaining myself very well but here is the code. I am experimenting with javascript by making a program that calculates square feet.

JS, in head

function myFunction()
{
    var x =0;
    var y =0;
    var z =0;

    x =document.getElementById("sqrft");
    y = document.getElementById("length");
    z = document.getElementById("width");
    if(x>0 && y>0)
    {
        x.value=y*z;
    }
}

HTML

<p>A function is triggered when the user releases a key in the input field. The function transforms the character to upper case.</p>
Enter your length: <input type="text" id="length" onkeyup="myFunction()">
Enter your width: <input type="text" id="width" onkeyup="myFunction()">
Enter your totalsqrft: <input type="text" id="sqrft" onkeyup="x.value">

So basically I am trying to make the value of "sqrft" change live as the value of length and width changes.

like image 242
user3542995 Avatar asked Mar 29 '26 20:03

user3542995


1 Answers

You need to use:

function myFunction() {
    var x = 0;
    var y = 0;
    var z = 0;

    x = document.getElementById("sqrft"); 
    y = document.getElementById("length").value; // get value of y
    z = document.getElementById("width").value;// get value of z
    if (y > 0 && z > 0) { // compare value of y and z instead of x and y
        x.value = y * z;
    }
}

Fiddle Demo

like image 92
Felix Avatar answered Apr 01 '26 06:04

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!