6

Sorry I'm rather new to coding in JavaScript. I'm trying to make a simple program that takes two inputs, for base and height, and outputs the area of a triangle.

My HTML is this:

<form name="input" action="" method="get">
        <label for="">Width (b): </label><input type="textbox" name="width"></input><br>
        <label for="">Height (h): </label><input type="textbox" name="height"></input><br>
        <button onClick="calculateArea()">Calculate area</button>
</form>

<label for="output">The area is: </label><input type="textbox" name="output"></input>

And my JavaScript is this:

function calculateArea() {

var base = document.getElementById('width').value;
var height = document.getElementById('height').value;
var out = (1/2) * parseFloat(base) * parseFloat(height);

document.output.value = out;

}

I've tried changing the name="output" of the output textbox to an ID and using getValueByID, but it has made no difference. Any help would be appreciated.

3 Answers 3

2

You have to get rid of form tag,than change javascript code a little Here is the working example: JSFIDDLE EXAMPLE

HTML:

<label for="">Width (b): </label><input type="textbox" name="width"></input><br>
<label for="">Height (h): </label><input type="textbox" name="height"></input><br>
<button onClick="calculateArea()">Calculate area</button><br>
<label for="output">The area is: </label><input type="textbox" name="output"></input>

JAVASCRIPT:

function calculateArea() {
var base = document.getElementsByName('width')[0].value;
var height = document.getElementsByName('height')[0].value;
var out = (1/2) * parseFloat(base) * parseFloat(height);

document.getElementsByName('output')[0].value= out;

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

2 Comments

That worked brilliantly and I can see what you were going for with it too. I'm not sure how the form tag was messing it up, but it's pristine now, so thank you!
The form doesn't matter. The important part is document.getElementById for the output box.
1

html:

<label for="">Width (b): </label>
<input type="textbox" name="width" id="width" value="2"/><br />
<label for="">Height (h): </label>
<input type="textbox" name="height" id="height" value="3"/><br />
<input type="button" id="calcButton" value="Calculate area" />
<br/>
<label for="output">The area is: </label>
<input type="textbox" name="output" id="output"/>

js:

document.getElementById('calcButton').onclick = calculateArea;

function calculateArea() {
    var base = document.getElementById('width').value;
    var height = document.getElementById('height').value;
    var out = (1/2) * parseFloat(base) * parseFloat(height);
    document.getElementById('output').value = out;
}

here is a fiddle ...

Comments

1

To get to an input element you need to use document.<formName>.<inputName>, therefore:

Put the output inside the form

<form name="input" action="" method="get">
    <label for="">Width (b): </label><input type="textbox" name="width" /><br />
    <label for="">Height (h): </label><input type="textbox" name="height" /><br />
    <button onClick="calculateArea(); return false;">Calculate area</button><br />

    <label for="">The area is: </label><input type="textbox" name="output" />
</form>

function calculateArea() {
    var base = document.input.width.value;
    var height = document.input.height.value;
    var out = (1/2) * parseFloat(base) * parseFloat(height);

    document.input.output.value = out;
}

JSBin

Note: This is all assuming you don't want to use ID's.

3 Comments

I can use IDs if I want, but this doesn't appear to work, it eats the inputs but displays no output.
I have, I copied it directly into my code but when I put in inputs and hit calculate, the only change is that the inputs disappear, no output is displayed, not even briefly.
Added a link to JSBin. Also, the form is submitted when you press the button, so I've added return false; in the onClick event to prevent that.

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.