0

Is it possible to fire backspace event on textbox using Javascript? Which should clear the character left to the cursor position. I know that event code for backspace is 8 but I don't know how to fire it using the code.

Solution specific to Internet Explorer 8 would be of great help.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TechnoCrat
  • 2,055
  • 4
  • 23
  • 35

2 Answers2

0

Have a look at this related question: Simulate JavaScript Key Events

Personally, I think it would be a better, cleaner approach to edit the text directly, removing the char before the cursor position. See How to get caret position in textarea

Community
  • 1
  • 1
Daniel S
  • 456
  • 4
  • 17
-2

Would be a lot easier to simply trim the value by one character from the end..

var TheTextBox = document.getElementById("id");
TheTextBox.value = TheTextBox.value.substring(0, TheTextBox.length - 1);
Koen Hollander
  • 1,687
  • 6
  • 27
  • 42
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • textbox.innerHtml == undefined – timidboy Aug 31 '12 at 10:31
  • You're supposed to populate that value yourself! It just needs to reference the textarea you want to update, I can't do it as you haven't provided any code/details about the textarea (ID mainly). – Dunhamzzz Aug 31 '12 at 10:32
  • Do mean textbox.value? doesn't posses 'innerHtml' property – timidboy Aug 31 '12 at 10:33
  • In that case, use `input.value`, I assumed textbox = textarea – Dunhamzzz Aug 31 '12 at 13:57
  • No cursor position can be anything. Let's say the text inside textbox is "Foo| Bar" where cursor is located after o then after firing thie backspace event text inside textbox should become "Fo Bar". This can't be achieved using value.substring(0, value.length - 1) – TechnoCrat Sep 04 '12 at 18:52