Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const input1 = document.getElementById('input1');
- const input2 = document.getElementById('input2');
- const btn = document.getElementById('btn');
- const res = document.querySelector(`.res`);
- input1.oninput = checkField;
- input2.oninput = checkField;
- input1.onchange = checkEmpty;
- input2.onchange = checkEmpty;
- // Удаляю буквы из полей
- // Останутся только цифры
- function checkField() {
- this.value = this.value.replace(/\D/g, ``);
- btn.disabled = !input1.value && !input2.value ?
- true : false;
- }
- // Проверяю, если оба поля заполнены - активирую кнопку
- // Иначе деактивирую
- function checkEmpty() {
- btn.disabled = !input1.value && !input2.value ?
- true : false;
- }
- btn.onclick = () => {
- // Привожу строки к числам и складываю
- const result = +input1.value + +input2.value;
- // В спан записываю результат
- res.textContent = result;
- // Деактивирую кнопку
- btn.disabled = true;
- // Стираю значения из полей
- input1.value = ``;
- input2.value = ``;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement