admin管理员组文章数量:1389768
I want to call a function whenever a key is pressed. I am using onKeyPress event handler. However i see that it calling the function only the first time the key is pressed. I want to call it everytime a key is pressed. Can someone help me in this?
I want to call a function whenever a key is pressed. I am using onKeyPress event handler. However i see that it calling the function only the first time the key is pressed. I want to call it everytime a key is pressed. Can someone help me in this?
Share Improve this question asked Sep 27, 2011 at 11:00 Raja RoyRaja Roy 753 silver badges4 bronze badges 3- my html code <input type="password" name="password" size=30 onkeyPress="checkPassword()" /> – Raja Roy Commented Sep 27, 2011 at 11:07
- Edit your post and add checkPassword() function contents. – antyrat Commented Sep 27, 2011 at 11:14
- The function checkPassword() merely checks whether the password is of a specified length. The problem is that the function is getting called only once instead of everytime a character in entered. – Raja Roy Commented Sep 27, 2011 at 11:21
4 Answers
Reset to default 2Probably a bit too late but...
<script>
function checkPassword(pwd){
if(pwd.length < 8){
document.getElementById('message').innerHTML = 'Password needs to 8 characters minimum.';
}
else{
document.getElementById('message').innerHTML = '';
}
}
</script>
In the HTML body...
<input type="password" name="password" size=30 onkeyup="checkPassword(this.value)" />
<span id="message"></span><br/>
Answer 1.
<head>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
var jq=jQuery.noConflict();
jq(document).ready( function(){
jq(document).keydown(function(event){
// -- here es your code of function --
jq("#keycode").html(event.which); // example code, event.which captures key index
});
});
</script>
</head>
<body>
<div id="keycode">Press any Key to see its index</div>
</body>
Answer 2.
You should replace onKeyPress with onchange
| http://www.w3schools./jsref/event_onchange.asp
I ran your HTML code and it works as expected.
Your checkPassword()
function may be set to do something only if the password is of a specified length, as in this code:
<input type="password" name="password" size=30 onkeyPress="checkPassword(this)" />
<script type="text/javascript">
function checkPassword(pass) {
if (pass.value.length > 7) {
alert("Password is greater than 7 characters.");
} else {
//DO NOTHING
}
}
</script>
In this case, if the password isn't greater than 7 characters, the function may seem like it isn't getting called, but it actually is (the function just runs so fast you don't even know that it's getting called).
<script>
function l(ths){
document.getElementById('length').innerHTML=ths.length;
}
</script>
Length: <span id="length"></span><br/>
<input type="text" onkeyup="l(this.value);"/>
本文标签: Calling a javascript function everytime a key is pressed in input fieldStack Overflow
版权声明:本文标题:Calling a javascript function everytime a key is pressed in input field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744664070a2618423.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论