Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript calculation with 2 variables

I have a problem with my form calculating.

This is my javascript:

function kembali()

var total=document.getElementById('total1').value;
var bayar=document.getElementById('bayar1').value;
document.getElementById('kembalian1').value=bayar*total;

And this is my html :

<form><table><tr>
    <td>Total</td>
    <td>:</td>
    <td><input type="text" name="total" id="total1" value="50000"/></td>
</tr>
<tr>
    <td>Bayar</td>
    <td>:</td>
    <td><input type="text" name="bayar" id="bayar1" onkeyup="kembali()" autocomplete="off"/></td>
</tr>
<tr>
    <td>Kembalian</td>
    <td>:</td>
    <td><input type="text" name="kembalian" id="kembalian1"/></td>
</tr></table></form>

I use this code to jsfiddle.net and didn't work. How to fix this? Can you help me? Thank you.

Here's the example

like image 458
Ramadhani Pratama Avatar asked Jul 13 '15 04:07

Ramadhani Pratama


People also ask

How do you sum two variables in JavaScript?

Basic JavaScript Addition The following code adds two numbers and stores the result in a variable named "sum": var x = 1; var y = 2; var result = x + y; The result is "3" in this simple example. Add numbers in JavaScript by placing a plus sign between them.

How do you perform the calculation by JavaScript?

const number1 = parseFloat(prompt ('Enter the first number: ')); const number2 = parseFloat(prompt ('Enter the second number: ')); let result; // declaration of the variable. // use if, elseif and else keyword to define the calculator condition in JavaScript.

What is and $$ in JavaScript?

$ and $$ are valid variable names in JavaScript, they have no special meaning. Usually they set their value to library instances, in your example if you check the closure call, at the end of the file you'll see that $ is jQuery in this case if it is defined and $$ is cytoscape.

Can you have two variables with the same name JavaScript?

In Javascript, you can't actually have 2 variables made with different goals and have the same name in the same scope . Basically, there're 2 types of scope: global and local . Global scope is what you're facing making 2 variables with the same name in different files.


1 Answers

you need to select option onLoad to No Wrap in head from drop down under Frameworks & Extensions.

update FIDDLE

jsfiddle documentation

Second drop-down is about the Code Wrap with default setting to onLoad.

There are 4 options to choose:

onLoad:

wrap the code so it will run in onLoad window event

onDomReady:

wrap the code so it will run in onDomReady window event

no wrap - in <head>:

do not wrap the JavaScript code, place it in <head> section

no wrap - in <body>:

do not wrap the JavaScript code, place it in <body> section

like image 87
Nishit Maheta Avatar answered Oct 11 '22 09:10

Nishit Maheta