Skip to content Skip to sidebar Skip to footer

Sum Of Two Variables From Div

I want to sum the two field automatically using javascript , After searching more about what I want exactly I found this example on table please how can I change it to work on divs

Solution 1:

Don't know exactly what you want but this does work.If you want from only div

<div id="std-1">1</div>
<div id="std-2">2</div>
<div>{{one + two}}</div>
</div>
 //scripts
 $scope.one=parseInt(document.getElementById('std-1').innerHTML);
 $scope.two=parseInt(document.getElementById('std-2').innerHTML);

or you don't mind input fields and making it dynamic

<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app><divclass="std-1">
     Student one: <inputng-model="student.one "type="number"/></div><divclass="std-2">
     Student two:  <inputng-model="student.two "type="number"/></div><div>Sum: {{student.one + student.two}}</div></div>

Solution 2:

Make it simple

<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app><divid="first"><inputng-model="first"placeholder="First number"type="text" /></div><divid="second"><inputng-model="second"placeholder="Second number"type="text" /></div><h1> Sum: {{first--second}} </h1></div>

Post a Comment for "Sum Of Two Variables From Div"