Breadit Breadit - 6 months ago 6
HTML Question

Changing Input Text Value JS

Breadit here and I have a problem. I tried to look everywhere but can't find a single answer that will work. I am trying to make a button which will square the first tag and will print the output on the second tag.

JavaScript:

function square() {
var a=document.getElementById("inputSquare");
var b=a*a;
document.getElementById("outputSquare").value=b;
}


HTML:

<input type="number" id="inputSquare">
<button onclick="square()">=</button>
<input type="number" id="outputSquare" readonly>


That is my problem.

Answer

Hey Breadit with a problem. Try this:

function squaree() {
    var a=document.getElementById("inputSquare").value;
    var b=a*a;
    document.getElementById("outputSquare").value=b;
}

Also. you have to put the code in <script> tag inside head. Not in dom load or jquery ready function. Check it out here: https://jsfiddle.net/q7oms4nh/

Comments