admin管理员组文章数量:1355542
I have used two events KEYPRESS AND ONBLUR in my input field, when the ENTER key pressed, it triggers KEYPRESS and ONBLUR as well.
Code:
<div id="text">
<input type="text" onblur="onBlur()" onkeypress="myFunction(event)">
</div>
window.myFunction = function(e) {
if (e.which == 13) {
document.getElementById("text").innerHTML = "text";
console.log("keypress");
}
}
window.onBlur = function() {
console.log("blur");
}
fiddle
Note:
I need to handle keypress as well onblur event, But i need only one event should be triggered at a time.
For example:
- When I press enter key, myFunction should be triggered. Not onBlur.
- When I click outside of the input field, onBlur function should be called.
Question:
how to prevent ONBLUR event, when ENTER key is pressed.
Your suggestion will be grateful
I have used two events KEYPRESS AND ONBLUR in my input field, when the ENTER key pressed, it triggers KEYPRESS and ONBLUR as well.
Code:
<div id="text">
<input type="text" onblur="onBlur()" onkeypress="myFunction(event)">
</div>
window.myFunction = function(e) {
if (e.which == 13) {
document.getElementById("text").innerHTML = "text";
console.log("keypress");
}
}
window.onBlur = function() {
console.log("blur");
}
fiddle
Note:
I need to handle keypress as well onblur event, But i need only one event should be triggered at a time.
For example:
- When I press enter key, myFunction should be triggered. Not onBlur.
- When I click outside of the input field, onBlur function should be called.
Question:
how to prevent ONBLUR event, when ENTER key is pressed.
Your suggestion will be grateful
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 2, 2017 at 17:24 RanjithKumar SVRanjithKumar SV 2352 gold badges4 silver badges13 bronze badges 3-
Just reset the property:
onblur = ""
– Scott Marcus Commented Feb 2, 2017 at 17:26 - 1 See my updated answer for the solution and tips to write the code in a more modern and clean way. – Scott Marcus Commented Feb 2, 2017 at 17:39
- @ScottMarcus Thanks – RanjithKumar SV Commented Feb 2, 2017 at 17:41
1 Answer
Reset to default 6You can just reset the property to no longer hold a reference to the previously stored callback function with onblur = ""
.
Also, don't set up your functions as properties of window
, which makes them global. Instead just declare them in the scope you want them accessible in.
However, you really should not be setting up event handlers using HTML attributes as they create spaghetti code, cause global wrapper functions to be created around your handler functions that alter this
binding and don't follow W3C DOM Event standards.
This code really should use the .addEventListener()
and .removeEventListener()
model:
// Wait until the DOM is loaded
window.addEventListener("DOMContentLoaded", function(){
// Get DOM reference to the input element in question
var input = document.getElementById("myInput");
// Attach callbacks to element for various events
input.addEventListener("blur", doBlur);
input.addEventListener("keypress", doKeypress);
// By declaring your functions inside a larger function, they avoid
// bee global functions and have the appropriate scope.
function doKeypress(e) {
// Check for ENTER key
if (e.keyCode === 13) {
// Remove blur event handler
input.removeEventListener("blur", doBlur);
// Do actions
document.getElementById("text").textContent = "text";
}
}
function doBlur() {
console.log("blur");
}
});
<div id="text">
<input type="text" id="myInput">
</div>
本文标签: javascripthow to prevent ONBLUR event when keypress or enterStack Overflow
版权声明:本文标题:javascript - how to prevent ONBLUR event when keypress or enter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743951918a2567446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论