For the numeric input element, is there a way to check if there is only a minus input? I can do this in a bad way in the example below. I'm looking for a more precise, simple method that I can access via the element.
When input is only -
=> numberInput.value
equals to empty string also innerText and innerHTML equals to empty string.
var lastKey;
var checkNegativeSign = function() {
let numberInput = document.querySelector("input");
if(lastKey === '-') {
document.getElementById("result").innerText = "is negative";
} else {
document.getElementById("result").innerText = "";
}
}
var onKeyUp = function(event) {
// Should check Escape, ctrl etc.
lastKey =event.key;
}
<html>
<body>
<input type="number" onkeyup="onKeyUp(event)" />
<button onclick="checkNegativeSign()">Check Negative Sign</button>
<div id="result"> </div>
</body>
</html>