admin管理员组文章数量:1403145
When the textarea is in active use (meaning, a user is typing keystores in this field), if the sequence of characters being typed matches a defined sequence like "aeu" then, I would like to set focus out of the textarea and click a button.
Note: I am using the Mousetrap plugin to handle keyboard shortcuts. .min.js
Demo: /
As you can see in the demo page, I have tried blur()
and trigger('blur')
but, I wasn't able to remove focus out of the textarea.
When the textarea is in active use (meaning, a user is typing keystores in this field), if the sequence of characters being typed matches a defined sequence like "aeu" then, I would like to set focus out of the textarea and click a button.
Note: I am using the Mousetrap plugin to handle keyboard shortcuts. https://github./ccampbell/mousetrap/blob/master/mousetrap.min.js
Demo: http://jsfiddle/PqXWJ/5/
As you can see in the demo page, I have tried blur()
and trigger('blur')
but, I wasn't able to remove focus out of the textarea.
- blur should get the job done. What's happening is the mouseTrap plugin isn't catching the keystrokes. – Kevin B Commented Jun 17, 2013 at 20:16
- The mousetrap plugin works fine when the focus is not on the textarea. – Srikanth AD Commented Jun 17, 2013 at 20:18
- Right. while focus is on the textarea, keypress keyup and keydown events aren't reaching the document which is a requirement for the Mousetrap plugin to function. – Kevin B Commented Jun 17, 2013 at 20:18
- So, do you have any suggestions? also, I can skip the mousetrap plugin if I can handle - detecting a bination of keystores by other means. – Srikanth AD Commented Jun 17, 2013 at 20:20
- Sorry, i was wrong. There's a bug somewhere in the mousetrap that's causing it to reset the current sequence on each keypress when in a textarea. I would just not use the plugin and roll my own. jsfiddle/PqXWJ/18 – Kevin B Commented Jun 17, 2013 at 20:43
4 Answers
Reset to default 4I am not sure what that library you are using is but I got the functionality you wanted using some simple jQuery event binding.
$('#text').keyup(function () {
if ($(this).val() === 'aeu') $(this).blur();
$('#myBtnId').click();
});
Here is an example in jsFiddle.
UPDATE:
Here is a reworked example using a text area and allowing for any number of characters to be entered before the desired string. Example
UPDATE 2:
Added the button functionality you requested. Cheers!
jsFiddle
You can do that without plugin -
$('textarea').on('keyup', function () {
if (this.value === 'enter') {
$(this).blur();
}
});
Demo ---->
http://jsfiddle/PqXWJ/5/
I think this is what you might be looking for
<textarea id="upcinput"></textarea>
<p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will un <a id="exception-btn">click here</a></p>
$('#upcinput').keyup(function () {
if ($(this).val() === 'lorem ipsum') {
$(this).blur();
$('#exception-btn').click();
}
});
http://jsfiddle/ZYJxq/4/
The mousetrap plugin you are using seems to have a bug with textareas, though since this is such an easy task, it would be quicker to roll my own than fix the broken plugin.
http://jsfiddle/PqXWJ/18/
var sequence = [], targetsequence = [65,69,85,13];
$(document).on("keyup",function(e) {
if (e.which == targetsequence[sequence.length]) {
sequence.push(e.which);
if (sequence.length == targetsequence.length) {
alert("Correct Sequence!");
$("#upcinput").blur();
sequence = [];
}
}
else {
sequence = [];
}
});
本文标签: javascriptHow to remove focus from textarea when it is in active useStack Overflow
版权声明:本文标题:javascript - How to remove focus from textarea when it is in active use - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744361394a2602562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论