0

The attached screenshot is my output. What I am trying to achieve is when I click AddWood, there will be 3 textbox generated (which is fine). the 3 textbox represent name, length and width. Once I hit calculate there should be a display below showing my the area, which is not working. Can anyone help to point out where my mistake is?

function addText() {
  var div1 = document.getElementById("div2");
  div1.innerHTML = "<input type='text' id=Wname/><br> <input type='text' id=length/><br>  <input type='text' id=width/>";
}

function myFunction() {
  var length = document.getElementById("length").value;
  var width = document.getElementById("width").value;
  var area = length * width;
  document.getElementById("Result").innerHTML = area;
}
<div id="div2"></div>
<input type="button" id="btnok" onclick="addText();" value="Add Wood"/>
<br />
<p><button onclick="myFunction()">Calculate</button>
<p id="Result"></p>

output

2 Answers 2

3

The id need to be inside the quotes. Also input is a self closing tag

function addText() {
  var div1 = document.getElementById("div2");
  div1.innerHTML = "<input type='text' id='Wname'/><br> <input type='text' id='length'><br>  <input type='text' id='width'>";
}

function myFunction() {
  var length = document.getElementById("length").value;
  var width = document.getElementById("width").value;
  var area = length * width;
  document.getElementById("Result").innerHTML = area;
}
<div id="div2"></div>

<input type="button" id="btnok" onclick="addText();" value="Add Wood" />
<br />
<p><button onclick="myFunction()">Calculate</button>
  <p id="Result"></p>

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer but I would like to mention parseInt() should be used on those values to avoid NAN result. Still +1
2

This line looks wrong to me, no inner quote marks around the id values

div1.innerHTML="<input type='text' id=Wname/><br> <input type='text' id=length/><br>  <input type='text' id=width/>";

Chuck it into a snippet, seems to do the trick.

function addText() {
  var div1 = document.getElementById("div2");
  div1.innerHTML = "<input type='text' id='Wname' /><br> <input type='text' id='length' /><br>  <input type='text' id='width' />";
}

function myFunction() {
  var length = document.getElementById("length").value;
  var width = document.getElementById("width").value;
  var area = length * width;
  document.getElementById("Result").innerHTML = area;
}
<div id="div2"></div>
<input type="button" id="btnok" onclick="addText();" value="Add Wood"/>
<br />
<p><button onclick="myFunction()">Calculate</button>
<p id="Result"></p>


Also...

It might be worth giving a look at https://developers.google.com/web/tools/chrome-devtools/ and debugging javascript (similar tools for all major browsers, just google).

Placing a breakpoint or even just some console.log(blah), console.info(blah) or console.error(blah) calls would be all you need to quickly resolve the vast majority of errors, or at the least provide better framed questions for even better help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.