i'm making a jquery calculator but so far I have it so only the numbers show up and the decimal when clicked. However, I need to make it so users are prevented from clicking decimal more than once, but am unsure on how to do so. I'm allowing users to type their numbers too, so I need to make it so when they click on the decimal button, it can only enter once, and when they enter the decimal on their keyboard (Using multiple keycodes like period keycode for decimal, depending on what user clicks on keyboard).
$(document).ready(function() {
$("#input").prop("disabled", true);
$(".number").click(function() {
var text = $('#input').val();
if (text != "0") {
var value1 = text;
var ButtonVal = $(this);
var value2 = ButtonVal.text();
var inputfield = value1 + value2;
$('#input').val(inputfield);
} else {
$('#input').val();
}
});
$(document).unbind('keypress').bind('keypress', function(e) {
var text1 = $('#input').val();
var text3 = text1;
if (e.keyCode == 48) {
var inputfield = text3 + "0"
$('#input').val(inputfield);
}
if (e.keyCode == 49) {
var inputfield = text3 + "1"
$('#input').val(inputfield);
}
if (e.keyCode == 50) {
var inputfield = text3 + "2"
$('#input').val(inputfield);
}
if (e.keyCode == 51) {
var inputfield = text3 + "3"
$('#input').val(inputfield);
}
if (e.keyCode == 52) {
var inputfield = text3 + "4"
$('#input').val(inputfield);
}
if (e.keyCode == 53) {
var inputfield = text3 + "5"
$('#input').val(inputfield);
}
if (e.keyCode == 54) {
var inputfield = text3 + "6"
$('#input').val(inputfield);
}
if (e.keyCode == 55) {
var inputfield = text3 + "7"
$('#input').val(inputfield);
}
if (e.keyCode == 56) {
var inputfield = text3 + "8"
$('#input').val(inputfield);
}
if (e.keyCode == 57) {
var inputfield = text3 + "9"
$('#input').val(inputfield);
}
if (e.keyCode == 110) {
var inputfield = text3 + "."
$('#input').val(inputfield);
}
if (e.keyCode == 190) {
var inputfield = text3 + "."
$('#input').val(inputfield);
}
if (e.keyCode == 46) {
var inputfield = text3 + "."
$('#input').val(inputfield);
}
})
});
.threadbit .thread {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.divTable {
display: table;
width: 20%;
text-align: center;
}
.divTableRow {
display: table-row;
}
.divTableCell,
.divTableHead {
border: 1px solid #999999;
display: table-cell;
padding: 3px 10px;
}
.divTableBody {
display: table-row-group;
}
button {
width: 60px;
height: 60px;
text-align: center;
line-height: 1.1em;
font-size: 1.1em;
border-color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<form>
<input type="text" name="firstname" style="width:404px;height:60px;font-size:20px;" id="input"><br>
</form>
<div class="divTable">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick="">sin</button></div>
<div class="divTableCell"><button type="text" onclick="">cos</button></div>
<div class="divTableCell"><button type="text" onclick="">tan</button></div>
<div class="divTableCell"><input type="radio" name="mode" value="deg">Deg</div>
<div class="divTableCell"><input type="radio" name="mode" value="rad">Rad</div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick="">sin<sup>-1</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">cos<sup>-1</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">tan<sup>-1</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">π</button></div>
<div class="divTableCell"><button type="text" onclick="">e</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick="">x<sup>y</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">x<sup>3</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">x<sup>2</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">e<sup>x</sup></button></div>
<div class="divTableCell"><button type="text" onclick="">10<sup>x</sup></button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick=""><sup>y</sup>√x</button></div>
<div class="divTableCell"><button type="text" onclick=""><sup>3</sup>√x</button></div>
<div class="divTableCell"><button type="text" onclick="">√x</button></div>
<div class="divTableCell"><button type="text" onclick="">In</button></div>
<div class="divTableCell"><button type="text" onclick="">log</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick="">(</button></div>
<div class="divTableCell"><button type="text" onclick="">)</button></div>
<div class="divTableCell"><button type="text" onclick="">1/x</button></div>
<div class="divTableCell"><button type="text" onclick="">%</button></div>
<div class="divTableCell"><button type="text" onclick="">n!</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button class="number" type="text" value="7" onclick="">7</button></div>
<div class="divTableCell"><button class="number" type="text" value="8" onclick="">8</button></div>
<div class="divTableCell"><button class="number" type="text" value="9" onclick="">9</button></div>
<div class="divTableCell"><button type="text" onclick="">+</button></div>
<div class="divTableCell"><button type="text" onclick="">Back</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button class="number" type="text" value="4" onclick="">4</button></div>
<div class="divTableCell"><button class="number" type="text" value="5" onclick="">5</button></div>
<div class="divTableCell"><button class="number" type="text" value="6" onclick="">6</button></div>
<div class="divTableCell"><button type="text" onclick="">−</button></div>
<div class="divTableCell"><button type="text" onclick="">Ans</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button class="number" type="text" value="1" onclick="">1</button></div>
<div class="divTableCell"><button class="number" type="text" value="2" onclick="">2</button></div>
<div class="divTableCell"><button class="number" type="text" value="3" onclick="">3</button></div>
<div class="divTableCell"><button type="text" onclick="">×</button></div>
<div class="divTableCell"><button type="text" onclick="">M+</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button class="number" type="text" value="0" onclick="">0</button></div>
<div class="divTableCell"><button class="number" type="text" value="." onclick="">.</button></div>
<div class="divTableCell"><button type="text" onclick="">EXP</button></div>
<div class="divTableCell"><button type="text" onclick="">÷</button></div>
<div class="divTableCell"><button type="text" onclick="">M-</button></div>
</div>
<div class="divTableRow">
<div class="divTableCell"><button type="text" onclick="">±</button></div>
<div class="divTableCell"><button type="text" onclick="">RND</button></div>
<div class="divTableCell"><button type="text" onclick="">AC</button></div>
<div class="divTableCell"><button type="text" onclick="">=</button></div>
<div class="divTableCell"><button type="text" onclick="">MR</button></div>
</div>
</div>
</div>