I want to prevent input focus onclick and focus inputs on double click.
Something like....
$('input').click(function) {
$(this)preventFocus();
});
$('input').dblclick(function) {
$(this)focus();
});
Or maybe I should use an if statement?
I want to prevent input focus onclick and focus inputs on double click.
Something like....
$('input').click(function) {
$(this)preventFocus();
});
$('input').dblclick(function) {
$(this)focus();
});
Or maybe I should use an if statement?
Share Improve this question asked Oct 30, 2013 at 20:38 colmtuitecolmtuite 4,49111 gold badges47 silver badges70 bronze badges 5- 1 try $(this).blur() instead of $(this)preventFocus() – Howard Renollet Commented Oct 30, 2013 at 20:41
- Have you actually tried anything? check out: stackoverflow./questions/8735764/… – Mike Edwards Commented Oct 30, 2013 at 20:42
- Been trying for 2 hours mate :( I'll take a look at that answer, thanks. – colmtuite Commented Oct 30, 2013 at 20:45
- Sorry to hear that :( You should try to phrase your questions in terms of 1) this is what I did and 2) this is the problem I'm having. The code you posted is not valid Javascript so as written you would get a syntax error, which you didn't mention. – Mike Edwards Commented Oct 30, 2013 at 20:47
- Ok thanks for tips. Will take them into account in future. – colmtuite Commented Oct 30, 2013 at 21:00
2 Answers
Reset to default 5I guess something like this should work :
$('input').on({
focus: function() {
if (!$(this).data('disabled')) this.blur()
},
dblclick: function() {
$(this).data('disabled', true);
this.focus()
},
blur: function() {
$(this).data('disabled', false);
}
});
FIDDLE
You could try something like the following
HTML:
<input type="text" id="myInput" readonly="readonly" />
jQuery:
$("#myInput").dblclick(function(){
$(this).prop("readonly", false)
});
$("#myInput").blur(function(){
$(this).prop("readonly", true);
});
See http://jsfiddle/KWuR5/