Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const input1 = document.getElementById('input1');
  2. const input2 = document.getElementById('input2');
  3. const btn = document.getElementById('btn');
  4. const res = document.querySelector(`.res`);
  5.  
  6. input1.oninput = checkField;
  7. input2.oninput = checkField;
  8. input1.onchange = checkEmpty;
  9. input2.onchange = checkEmpty;
  10.  
  11. // Удаляю буквы из полей
  12. // Останутся только цифры
  13. function checkField() {
  14.  this.value = this.value.replace(/\D/g, ``);
  15.  btn.disabled = !input1.value && !input2.value ?
  16.     true : false;
  17. }
  18.  
  19. // Проверяю, если оба поля заполнены - активирую кнопку
  20. // Иначе деактивирую
  21. function checkEmpty() {
  22.   btn.disabled = !input1.value && !input2.value ?
  23.     true : false;
  24. }
  25.  
  26. btn.onclick = () => {
  27. //   Привожу строки к числам и складываю
  28.   const result = +input1.value + +input2.value;
  29. //   В спан записываю результат
  30.   res.textContent = result;
  31. //   Деактивирую кнопку
  32.   btn.disabled = true;
  33. //   Стираю значения из полей
  34.   input1.value = ``;
  35.   input2.value = ``;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement