1

guys, I have problems with a basic calculator js object only to add numbers input1 + input2 = input3. I have no clue how to display the result for input 1 and 2 in input 3 ( result ). From now I just get value from inputs using show('1'). This is my code

JS

function calculator(){
    this.result = document.getElementById('3');
    this.show = function(id){
        var a =document.getElementById(id).value
        console.log(a);
    }
}

HTML

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>
<label for="1">
<input type="text" id = '1'>
</label>
<br><br>
<label for="2">
<input type="text" id = '2'>
</label>
<br><br>  
<label for="3">
<input type="text" id = 3>
</label>
<button onclick="click()">+</button>
  <script src="jss.js"></script>

</body>

</html>
2
  • 1
    where is your click() function? Commented Dec 18, 2018 at 11:58
  • 1
    ´id`s shouldn't be numbers. The should start with a letter or an underscore. Commented Dec 18, 2018 at 11:59

4 Answers 4

3

There are several issues in your code. There is no click() defined in your code, it should be calculator(). You have to covert the value before the calculation. Otherwise string concatenation will happen as the default type of input value is string. You can use parseFloat() to do that.

Try the following way:

function calculator(){
    document.getElementById('3').value = parseFloat(document.getElementById('1').value) + parseFloat(document.getElementById('2').value)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label for="1">
<input type="text" id = '1'>
</label>
<br><br>
<label for="2">
<input type="text" id = '2'>
</label>
<br><br>  
<label for="3">
<input type="text" id = '3'>
</label>
<button onclick="calculator()">+</button>

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

Comments

2

function calculator() {
  var item1 = parseFloat(document.getElementById('item1').value);
  var item2 = parseFloat(document.getElementById('item2').value);
  document.getElementById('item3').value = item1 + item2;
}
<input type="number" id="item1" value="0" oninput="calculator()">
<br><br>
<input type="number" id="item2" value="0" oninput="calculator()">
<br><br>
<input type="text" id="item3" disabled>
<button onclick="calculator()">+</button>

Comments

2

Call your function calculator() rather than click(), which actually isn't a function you've set up.

<button onclick="calculator()">+</button>

Comments

2

You can do it like this

You need to grab both the input fields. Extract value from them change them to number and than after addition add it to third input.

function calculator(){
    let result = document.getElementById('3');
    let input1 = document.getElementById('1').value;
    let input2 = document.getElementById('2').value;
    let final = Number(input1) + Number(input2);
    result.value = final;
}
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>
<label for="1">
<input type="text" id = '1'>
</label>
<br><br>
<label for="2">
<input type="text" id = '2'>
</label>
<br><br>  
<label for="3">
<input type="text" id = 3>
</label>
<button onclick="calculator()">+</button>

</body>

</html>

Comments

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.