-1

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.

  • You need value, not element it self, for var a.... – sinisake May 29 '16 at 18:18
  • Let's see, I'm sure you fired up the debugger and walked through this step by step, right? So you placed a breakpoint on the `var b=a*a` line, and there you examined the value of `a`, and/or tried typing `a*a` into the console to see what it evaluated to, right? Anyway, you say "That is my problem", but we don't know what your problem is--what is it? What does this code do, vs. what do you want it to do? –  May 29 '16 at 18:57

2 Answers2

0

need to have var a = document.getElementById("inputSquare").value

EDIT: What nevermind said.

Afrocious
  • 109
  • 1
  • 1
  • 5
0

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/

Martin Gottweis
  • 2,721
  • 13
  • 27