JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<input id="input1" type="text">
<br>
<br>
<input id="input2" type="text">
<br>
<br>
<button>Сложить</button>
</body>
JavaScript
var inputAll = document.querySelectorAll('input');
var button = document.querySelector('button');
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var p = document.createElement('p');
p.style.color = 'red';
function errorCheck() {
for(var i = 0; i < inputAll.length; i++) {
console.log(inputAll[i].value)
if (isNaN(+inputAll[i].value)) {
p.innerHTML = 'error';
insertAfter(p, inputAll[i]);
}
}
}
button.addEventListener('click', function count() {
p.innerHTML = '';
var answer = +inputAll[0].value + +inputAll[1].value;
if (isNaN(answer)) {
errorCheck();
return;
}
alert(answer);
});