0

What I am trying is the following : I have 2 buttons and one textfield (code you see below) On the buttons there is a value on it : 0,20 and 0,05. When you press one of the buttons the value should be displayed in the textfield and when you press one of those buttons again the value should be added to the current value. Here is the code I have at the moment :

<input id="bedraggroot" type="button"  value="0.20" onClick="b();">   
<input  id="bedragklein" type="button" value= "0.05" onClick="b();">

<p> Ingeworpen : <span id="toonbedrag"> Ingeworpen </span></p>

function b()
{
var bedrag1 = parseFloat(document.getElementById('bedraggroot').value);
var bedrag2 = parseFloat(document.getElementById('bedragklein').value);
var totaalbedrag;

if(bedrag1 == 0)
  {
    parseFloat(totaalbedrag)+ bedrag2;
  }

if(bedrag2 == 0)
  {
    parseFloat(totaalbedrag) = totaalbedrag + bedrag1;
    tussenbedrag = tussenb
  }



document.getElementById('toonbedrag').innerHTML = totaalbedrag;
}

I already tried al bunch of stuff but nothing seem to work what I try. without the parseFloat, with a + before it. (Read all those things in these forums) Can someone help me out?

As you might know, I am just a beginner in these things.

Kind regards.

4 Answers 4

1

Instead of doing the if conditions, you should pass the button values to the function "b" (you should use more definitive names for the functions).

You are also doing the calculation wrong. "a + b" doesn't really store the result of the calculation, but "a = a + b;" does.

<input id="bedraggroot" type="button"  value="0.20" onClick="b(this.value);">   
<input  id="bedragklein" type="button" value= "0.05" onClick="b(this.value);">

<p> Ingeworpen : <span id="toonbedrag"> Ingeworpen </span></p>

<script>

var totaalbedrag = 0;

function b(bedrag)
{

    totaalbedrag = parseFloat(bedrag) + totaalbedrag; 
    document.getElementById('toonbedrag').innerHTML = totaalbedrag.toFixed(2);

}

</script>

"toFixed" removes the issue with float decimal rounding, described here: Javascript, weird floating point number endless decimal?

Try it out in here in jsfiddle

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

2 Comments

Oh shoot, I was like 3 minutes late.
thanks!! this does the trick! love the quick replies always here on this website! I keep on learning
1

lots of issues here. You're not assigning parseFloat(totaalbedrag)+ bedrag2; and you're assigning something to the expression parseFloat(totaalbedrag) which should definitely fail and display something in the console.

Finally, you're assigning tussenb to tussenbedrag but neither was defined.

Comments

1

If the code you show above is complete - you're missing the script tag to mark it as code. See http://www.w3schools.com/html/html_scripts.asp

Comments

0

Please try below

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery 3D Falling Leaves Demo</title>
<link href="css/bootstrap.css" type="text/css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
</head>
<body>
<input id="bedraggroot" type="button"  value="0.20" onClick="b(this.value);">
<input  id="bedragklein" type="button" value= "0.05" onClick="b(this.value);">
<p> Ingeworpen :
  <input type="text" id="toonbedrag" value="0.0" />
</p>
<script>
function b(value)
{
var prevTotal = $('#toonbedrag').val();

var total=parseFloat(prevTotal) + parseFloat(value);
 $('#toonbedrag').val(total.toFixed(2));
}
</script>
</body>
</html>

3 Comments

Thanks alot for the answer! but I am learning Javascript at the moment. (Even though those 2 are very close together)
@user3742078 What do you mean? Is it working fine or any correction needed?
@user3742078 understand.. leave it

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.