I am Still messing with this calculator coded in JavaScript. I’m asking this question on how do I make it so that it stops showing an alert for the output and prints it into the div tag below named “Output”.
I’ve already placed in what I think it would be in the Output and I have pasted this below:
<html>
<head>
<link href="stylesheet/stylesheet.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function checkinputs(){
var A = parseInt(document.triangleform.input1.value);
var B = parseInt(document.triangleform.input2.value);
var C = parseInt(document.triangleform.input3.value);
if (A == B && B == C) { alert("Equalateral"); }
if (A == B && B != C || B == C && A != B || C == A && A != B) {alert("Isosceles");}
if (A != B && B != C && C != A) {alert("Scalene!");}
}
function keypress(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /[0-9]|\./;
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
function notEmpty(field, NocharMsg){
if(field.value.length == 0){
alert(NocharMsg);
field.focus();
return false;
}
return true;
}
</script
</head>
<body>
<div id="Container">
<div id="Header"><h1></h1></div>
<div id="Content_1">
<div id="Explanation">
This calculator will determine what
triangle you have made depending on
the integer values in the input fields.
</div>
<div id="Form">
<FORM NAME="triangleform">
Enter the triangle values below: <br>
<p>
<h4>Side 1: </h4><BR>
<INPUT TYPE="Integer" NAME="input1" onkeypress='keypress(event)' /><P>
<h4>Side 2: </h4><BR>
<INPUT TYPE="Integer" NAME="input2" onkeypress='keypress(event)' /><P>
<h4>Side 3:</h4> <BR>
<INPUT TYPE="Integer" NAME="input3" onkeypress='keypress(event)' /><P>
<INPUT TYPE="button" NAME="Submit" Value="Submit" onClick="checkinputs()" />
</FORM>
</div>
<div id="Verbal_Output">
<h2>You made a:</h2>
<p>
<h2>Triangle</h2>
</div>
</div>
<p>
<p>
<div id="Content_2">
<div id="Output">
<script>
document.getElementById("Output").value = checkinputs();
</script>
</div>
</body>
</html>
Thank you very much.
Nearly there – a div doesn’t have a
valueproperty (that’s for form inputs), but it does have ainnerHTMLproperty. Just make sure youreturnthe values you want fromcheckInputs(), instead ofalerting them.For example:
Then in your markup:
Note that
.innerHTMLis case-sensitive.