0

I would like to be able to do a quick calculation where I add the contents of a div and input. I can do this with 2 inputs but can't figure out how to do it this way. My attempts at coding have got me here:

var one = document.getElementById('one'),
  two = document.getElementById('calculate'),
  total = document.getElementById('total');
document.getElementById('calculate').onchange = function() {
  total.innerHTML = parseInt(one.innerHTML) + parseInt(two.innerHMTL);
};
<div id="one">11</div>

<input type=number value=2 id="calculate">

<div id="total">0</div>

I tried changing "parseInt(two.innerHTML)" to "parseInt(two)" but it was unsuccessful. I get the NotANumber/NaN error. Any help would be greatly appreciated.

2 Answers 2

2

since two in you case is an input tag it doesn't have the innerHTML. Use value attribute instead. so it'll look like this:

document.getElementById('calculate').onchange = function() {
    total.innerHTML = parseInt(one.innerHTML) + parseInt(two.value);
};
Sign up to request clarification or add additional context in comments.

Comments

0

To get value from a input tag use .value document.getElementById('input').value

So in your case try parseInt(two.value)

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.