Narmin
Narmin

Reputation: 69

how to I remove the last digit of the number in input javascript

   <!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>

<body>
<input id="txtBox" type="text" style="text-align: right;font-size:70px;width: 400px;height: 150px; font-family: bold;" />
<br />
<button onclick="Click(event)" id="clear" style="user-select: none; font-size:25px; width:98px ; height: 70px; font-family:bold;">C</button>
<button onclick="Click(event)" id="del" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">delete</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">+/-</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">+</button>

<br />

<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">1</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">2</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">3</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">-</button>

<br />

<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">4</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">5</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">6</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">*</button>

<br />

<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">7</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">8</button>
<button onclick="Click(event)" id="num" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">9</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">/</button>

<br />

<button onclick="Click(event)" id="zero" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">0</button>
<button onclick="Click(event)" id="ope" style="user-select: none;font-size:25px; width:98px ; height: 70px; font-family:bold;">.</button>
<button onclick="Click(event)" id="equalto" style="user-select: none;font-size:25px; width:200px ; height: 70px; font-family:bold;">=</button>

<script>
    var nums = document.getElementById("num")
    var operator = document.getElementById("ope")
    var textbox = document.getElementById("txtBox")
    var strng=document.getElementById("txtBox").value;

    function Click(e) {
        var but = e.target;
        if (but.innerText != "=") {
            textbox.value += but.innerText
        }
        else {
            textbox.value = eval(textbox.value)
        }
        if (but.innerText == "C") {
            textbox.value = ""
        }
        if(but.innerText=="delete"){
            textbox.value = textbox.value.substring(0, textbox.value.length - 1);
        }
    }
</script>
</body>

</html>

My homework is to make a calculator and I am stuck in making many things.I only put here 1 button because otherwise it would be hard to read the code.I only know (functions,variables,if,else,while and document) I can't delete only last digit,I need a code that only outputs one dot(like 0.555).Can somebody help me?

Upvotes: 0

Views: 1664

Answers (1)

Abhishek Mani
Abhishek Mani

Reputation: 522

You just need to update this in your code,

 if (but.innerText != "=" && but.innerText != "delete") {
        textbox.value += but.innerText
    }

your solution

Upvotes: 1

Related Questions