Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <form>
  2.   <input type="text" value="0">
  3.   <input type="text" value="0">
  4.   <select onchange="calcul()">
  5.     <option value="+">+</option>
  6.     <option value="*">*</option>
  7.   </select>
  8.   <div id="rezultate">asdf</div>
  9. </form>
  10.  
  11. <script>
  12.   var op = document.getElementsByTagName('select')[0];
  13.   op.addEventListener('change', calcul);
  14.  
  15.  
  16.   function calcul(){
  17.     var vals = document.getElementsByTagName('input');
  18.     var val1 = parseInt(vals[0].value);
  19.     var val2 = parseInt(vals[1].value);
  20.  
  21.  
  22.     op = op.options.selectedIndex;
  23.  
  24.     var result = op===0 ? val1 + val2 : val1 * val2;
  25.     var opr = op===0 ? '+' : '*';
  26.  
  27.     var resDiv = document.getElementById('rezultate');
  28.     resDiv.innerHTML = val1 + opr + val2 +'='+result;
  29.   }
  30. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement