Reputation: 4714
I have problem with the following code :
function KeyCheck(e) {
// ... Some Code
for (var i = 0; i < length; i++) {
width = elements[i].offsetWidth;
if(width > 245) {
e.preventDefault();
}
}
}
As you can see I have a loop, I want only elements that are less than 245px to have the event work, and the elements that are bigger than 245px not have it work.
The Problem - When the if 'meets' an element with width less than 245px it stops the loop and I think it exits the function also...
What can I do so it will continue looping and only elements less than 245px wide will get the event to work ?
Upvotes: 0
Views: 979
Reputation: 184
e.preventDefault(); should use in event callback function to set aside you defaul event i.e form submiting.
I your case you should execute some action for div small 145px
Upvotes: 0
Reputation: 13967
Is this what you're trying to do?: http://jsfiddle.net/D2xhz/
$("input").keydown(function(e){
if ($(this).width() > 245)
e.preventDefault();
});
The bottom two inputs cannot be typed in.
Upvotes: 1
Reputation: 95062
The code as-is will continue looping until the end of the loop unless preventDefault
is not defined on e
Upvotes: 4