shubbard
shubbard

Reputation: 5

adding div and input box

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.

Upvotes: 0

Views: 37

Answers (2)

Durga
Durga

Reputation: 15614

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

So in your case try parseInt(two.value)

Upvotes: 0

Guranjan Singh
Guranjan Singh

Reputation: 734

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);
};

Upvotes: 2

Related Questions