Reputation: 51
This code is working perfectly in Chrome, but it has some problem in Firefox (both newest version). See in Jsbin: http://jsbin.com/fasuxofavo/1/edit
$(document).ready(function(){
$(".typinganswer input").on('keypress', function(e)
{
if(e.keyCode==13){
alert("finish");
}
if($(this).val('') != "")
$(this).val('');
if($(this).is(':last-child'))
$(this).select();
$(this).next().select();
})
.on('keyup',function(e)
{
if(e.keyCode==8){
$(this).prev().select();
return;
}
})
$(".typinganswer input").on('click', function(e)
{
$(this).select();
});
})
Backspace is not working properly. Help me to fix this. See in Jsbin: http://jsbin.com/fasuxofavo/1/edit
Upvotes: 0
Views: 40
Reputation: 16041
The problem is, when you click Backspace, both handlers are called, you should just ignore the Backspace in the keypress
handler. Put this to the start of the callback:
$(".typinganswer input").on('keypress', function (e) {
if (e.keyCode == 8) {
return;
}
...
});
Or if you want to use many different keys, I would recommend using a switch
statement on the keyCode
.
Upvotes: 1
Reputation: 2292
if(e.keyCode!=8){...}
around your keypress event actions like so:
$(document).ready(function(){
$(".typinganswer input").on('keypress', function(e)
{
if(e.keyCode!=8){
if(e.keyCode==13){
alert("finish");
}
if($(this).val('') !== "")
$(this).val('');
if($(this).is(':last-child'))
$(this).select();
$(this).next().select();
}
})
.on('keyup',function(e)
{
if(e.keyCode==8){
$(this).prev().select();
return;
}
});
$(".typinganswer input").on('click', function(e)
{
$(this).select();
});
});
Upvotes: 1