Poltergeist
Poltergeist

Reputation: 7

How to erase last digit in jquery

$('#delete').click(function(){
    return val.slice(0, -1);    
});

I want to create a C button in my calculator which can delete last entered digit..

Upvotes: 1

Views: 135

Answers (2)

Jai
Jai

Reputation: 74738

You can assign the new value after slicing:

$('#delete').click(function(){
    var v = $('targetInput').val().slice(0, -1);
    $('targetInput').val(v);
});

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

You can use val() with callback function to update the value based on old value

$('#delete').click(function() {
  $('#text').val(function(i, val) {
    return val.slice(0, -1);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="text" />
<button id="delete">C</utton>

Upvotes: 1

Related Questions