Problem JavaScript

willemvo

New Member
Messages
19
Reaction score
0
Points
0
HTML:
<html>
<head>
<title>Test JavaScript</title>
<script lang="JavaScript">
<!--
//********************************************\\
//******script made by willem van oort ©2008******\\
//*********************************************\\
bereken(form)
var purchase = form.purchase.value;
var selling price = form.selling price.value;
var bruto = selling price - purchase;
var driving costs = form.driving costs.value;
var left = form.left.value;
var production costs = form.production costs.value;
var netto = selling price - purchase - driving costs - left - production costs;
-->
</script>
<style lang="text/css">
p {
font-family:times new roman, arial;
}
h1 {
font-family:times new roman, arial;
}
</style>
</head>
<body>
<input type="text" name="purchase" value="purchase" size="20" onfocus="if (this.value=='purchase') {this.value=''}" onblur="if (this.value=='') {this.value='purchase'}"><br>
<input type="text" name="selling price" value="selling price" size="20" onfocus="if (this.value=='selling price') {this.value=''}" onblur="if (this.value=='') {this.value='selling price'}"><br>
<input type="text" name="production costs" value="production costs" size="20" onfocus="if (this.value=='production costs') {this.value=''}" onblur="if (this.value=='') {this.value='production costs'}"><br>
<input type="text" name="left" value="left" size="20" onfocus="if (this.value=='left') {this.value=''}" onblur="if (this.value=='') {this.value='left'}"><br>
<input type="text" name="driving costs" value="Rij-Kosten" size="20" onfocus="if (this.value=='Rij-Kosten') {this.value=''}" onblur="if (this.value=='') {this.value='Rij-Kosten'}"><br>
<input type="button" name="button" onClick="bereken(this.form)" value="bereken!">
<script lang="JavaScript">
<!--
//********************************************\\
//******script made by willem van oort ©2008******\\
//*********************************************\\
document.write("<br>purchase: €" +purchase);
document.write("<br>selling price: €" +selling price);
document.write("<br>Bruto profit: €" +bruto);
document.write("<br>production costs: €" +production costs);
document.write("<br>Rij-kosten: €" +driving costs);
document.write("<br>left: €" +left);
document.write("<br>Netto profit: €" +netto);
if(netto > 0){
 document.write("<p>There was profit");
} else if (netto == 0) {
 document.write("<p>There was no profit");
} else {
 document.write("<p>There was a negative profit");
}
-->
</script>
</body>
</html>
Can somebody tell me what i did wrong,, because i don't have a clue what i did wrong:dunno:
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
First, variables and element names can't have spaces. So "selling price" and "form.production costs.value" and the others like that are invalid.

Second, bereken() isn't defined anywhere. I'm going to assume that the first JS is supposed to be a definition for it. But in this case, you can't use document.write() to display the values because document.write() only works well during page load at which point the values couldn't have been entered by the user. Creating a <div> and using it's innerHTML property to display the result is much more feasible here.

Lastly, you need the <input>'s wrapped in a <form> in order to refer to their form(e.g., this.form).

Try this instead:

HTML:
<html>
<head>
<title>Test JavaScript</title>
<script lang="JavaScript">
<!--
//********************************************\\
//******script made by willem van oort ©2008******\\
//*********************************************\\
function bereken(form) {
    var purchase = form.purchase.value;
    var sellingprice = form.sellingprice.value;
    var bruto = sellingprice - purchase;
    var drivingcosts = form.drivingcosts.value;
    var left = form.left.value;
    var productioncosts = form.productioncosts.value;
    var netto = sellingprice - purchase - drivingcosts - left - productioncosts;
    var result = document.getElementById('result');
    result.innerHTML = '';
    result.innerHTML += "<br>purchase: €" +purchase;
    result.innerHTML += "<br>selling price: €" +sellingprice;
    result.innerHTML += "<br>Bruto profit: €" +bruto;
    result.innerHTML += "<br>production costs: €" +productioncosts;
    result.innerHTML += "<br>Rij-kosten: €" +drivingcosts;
    result.innerHTML += "<br>left: €" +left;
    result.innerHTML += "<br>Netto profit: €" +netto;
    if (netto > 0) {
        result.innerHTML += "<p>There was profit</p>";
    }
    else if (netto == 0) {
        result.innerHTML += "<p>There was no profit</p>";
    }
    else {
        result.innerHTML += "<p>There was a negative profit</p>";
    }
}
-->
</script>
<style lang="text/css">
p {
font-family:times new roman, arial;
}
h1 {
font-family:times new roman, arial;
}
</style>
</head>
<body>
<form>
<input type="text" name="purchase" value="purchase" size="20" onfocus="if (this.value=='purchase') {this.value=''}" onblur="if (this.value=='') {this.value='purchase'}"><br>
<input type="text" name="sellingprice" value="selling price" size="20" onfocus="if (this.value=='selling price') {this.value=''}" onblur="if (this.value=='') {this.value='selling price'}"><br>
<input type="text" name="productioncosts" value="production costs" size="20" onfocus="if (this.value=='production costs') {this.value=''}" onblur="if (this.value=='') {this.value='production costs'}"><br>
<input type="text" name="left" value="left" size="20" onfocus="if (this.value=='left') {this.value=''}" onblur="if (this.value=='') {this.value='left'}"><br>
<input type="text" name="drivingcosts" value="Rij-Kosten" size="20" onfocus="if (this.value=='Rij-Kosten') {this.value=''}" onblur="if (this.value=='') {this.value='Rij-Kosten'}"><br>
<input type="button" name="button" onClick="bereken(this.form)" value="bereken!">
</form>
<div id="result"></div>
</body>
</html>
 

EliteACE

New Member
Messages
9
Reaction score
0
Points
0
what exactly is your problem though? is it simply not loading? or nothing happens when pressing your buttons?
 
Last edited:
Top