How to remove only the last single character and not an integer value?

how to remove last character from tvalue? for example: the value was for tvalue = "001 212 777 3456"

now, when the keyboard is called ("BACKSPACE"), it should not be completely cleared, but only delete one character at the end, and not the whole.

function keyboard(input) {
  if (input==='BACKSPACE') {
    tvalue = '';
  } else if(input ==='QUOTE') {
    tvalue = tvalue + "'";
  } else if(input ==='SPACE') {
    tvalue = tvalue + " ";
  } else {
    tvalue = tvalue + input;
  }    
  $('#' + tinput).val(tvalue).trigger('input');

  console.log(">>> Keyboard: ", input);
}
keyboard('BACKSPACE');
console.log('Show me the input now? ' , tvalue);

Expected Result:

 001 212 777 345
 001 212 777 34
 001 212 777 3
 001 212 777 
 001 212 777
 001 212 77

Expected result: "Not empty, but only the last character."

+4
source share
2 answers

Are you looking for a substring?

tvalue.substring(0,(tvalue.length - 1));
+4
source

As simple as

tvalue = tvalue.slice (0, -1);

It basically chopped off the last character, exactly what you want.

+2
source

Source: https://habr.com/ru/post/1613547/


All Articles