-3

Is it possible to remove an input's content when it's focused by pressing a key?

Thank you in anticipation! :)

2
  • 3
    You know, I just bet it is possible; why don't you try it, and then come back with any issues? We like to help those that help themselves.
    – Evan Davis
    Jul 26, 2013 at 0:23
  • i don't meant pressing backspace...
    – Lumoris
    Jul 26, 2013 at 0:33

2 Answers 2

1

Yes. With jQuery you can do something like the following (see http://jsfiddle.net/5tsNW/1/ for a demo):

HTML:

<input type="text" id="testInp" />

JavaScript:

$(function() {
    $("#testInp").on("keydown", function (e) {
        // backspace is key code 8
        if(e.keyCode === 8) {
            $(this).val("");
        }
    });
});
1
  • ohh my god, i tried this a couple of times, but now it works :o thank you, anyway :D
    – Lumoris
    Jul 26, 2013 at 0:30
1

Try this:

$('input').keypress(function(event) {
    if ( event.which == 8 ) { // backspace
        $(this).val('');
    }
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.