Event is not defined error in javascript only Firefox - javascript

I use the following script to validate the text box to enter only numbers and (.) which means it is decimal textbox validation. It was work fine in Internet Explorer and Google Chrome. If I execute the function in FireFox I get the following Error:
Event Is not Defined.
How to solve this?
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
I call this function like this:
$('.decimalValidate').live('keypress',function(){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(decimalval);
if(decimalvalidate == false)
return false;
});
I assign this validation for text box like this:
input type="text" id="Total" class="abc"

Try this
function abc(event) {
if(!event)
event= window.event;
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
and
$('.decimalValidate').live('keypress',function(e){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(evt); //keypress event
if(decimalvalidate == false)
return false;
});

decimalval is not an Event object, and you have to pass it to the abc function in ordert to find out which key you pressed:
$('.decimalValidate').live('keypress',function(ev){
var decimalid=$(this).attr("id");
var decimalval=$('#'+decimalid).val();
var decimalvalidate=abc(ev);
if(decimalvalidate == false)
return false;
});

$('.decimalValidate').live('keypress',function(e){
var decimalvalidate=abc(e); //this will point to the event of the keypress.
if(decimalvalidate == false)
return false;
});
I am not sure why you did all of the decimalid and decimalval operations, but if you want the event, do as I wrote in the edited code above.
Good luck.

$('.decimalValidate').on('keypress',function(event){
var decimalid = $(this).attr("id");
var decimalval = $('#'+decimalid).val();
var decimalvalidate = abc(event);
if(decimalvalidate == false)
return false;
});
function abc(event) {
if (event.keyCode > 47 && event.keyCode < 58) {
return true;
}
if (event.keyCode == 8 || event.keyCode == 46)
{
return true;
}
return false;
}
It helps you..

Related

Need help restricting a js function(that restricts invalid chars) to a particular input type

Hi i would like to restrict a function that allows only, numbers, back space and left & right arrow keys to inputs with number type, because when i implement it, it also affects my text inputs.
<script>
function chars(evt){
var key = window.event ? event.keyCode : event.which;
if (event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
return true;
}
else if ( key < 48 || key > 57 ) {
return false;
}
else return true;
}
</script>
Assign an id to your <input>. Add an event listener to it, like :
function getKeyCode() {
var key = window.event ? event.keyCode : event.which;
if(event.keyCode == 8 || event.keyCode == 46
|| event.keyCode == 37 || event.keyCode == 39) {
console.log(true);
//return true;
} else if (key < 48 || key > 57) {
console.log(false);
// return false;
} else {
console.log(true);
// return true;
}
}
var el = document.getElementById("myInput");
el.addEventListener("keypress", getKeyCode);
<input type="text" id="myInput">

Disable enter key and space if iframe body is empty onload

How to check if iframe content is empty then then disable "enter key" and "space"
else if Enable.
DEMO HERE
var iframeContainerVal = $("#textEditor").contents().find("body").text();
var container = document.getElementById("textEditor").contentWindow;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keycode == 32 ) {
alert('aaa');
event.preventDefault();
}
});
}
if(iframeContainerVal == true) {
checkPress()
}
else {
alert('false')
}
Use return false;
function checkPress(){
alert('In Fn')
$(container).keypress(function(event){
if (event.keyCode == 10 || event.keyCode == 13 || event.keyCode == 32 ) {
return false;
}
});
}
you used event.keycode == 32 in your If statement But, it should capital C in keycode like
event.keyCode == 32
Check for iframe empty content , iframe id = 'if'
if ($('#iframe').contents().find('body').children().length == 0) {
alert('I frame is empty');
if (event.keyCode == 32 || event.keyCode == 13) {
return false;
}
}

Detect keycode ALT+L via Javascript

I try to detect if a user presses F12 or ALT + L.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode == 18 && event.keyCode == 76)) {
//do anything
return false;
}
}
What am I doing wrong?
event.keyCode contains only one value. You can use event.altKey do detect if the alt key is pressed.
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123 || (event.keyCode === 76 && event.altKey)) {
//do something
return false;
}
}
The standard way is to create a bool to detect if the 'alt' key is currently held down and then a separate if to detect if that is true and if the L key as just been pressed - see the fiddle:
http://jsfiddle.net/L4cb9/1
var held = false;
...
else if (event.keyCode == 18) {held = true;}
if (held == true && event.keyCode == 76) {
alert();
}
...
document.onkeyup = function(event) {
if (event.keyCode == 18) {held = false;}
}
This is applicable to holding any combination of keys - you can create an array for multiple key holds greater than two:
held = [];
...
if (event.keyCode == i) {held[i] = true;}
...
and so on

Not all code paths return a value (JavaScript)

document.getElementById('search_field').onkeypress = function(e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + $('#search_field').val();
return false;
}
};
The last bracket show me an error, not all code paths return a value, what seems to be problem here ?
Thanks
Try this :
document.getElementById('search_field').onkeypress = function(e) {
if (!e) {
e = window.event;
}
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + $('#search_field').val();
return false;
}
return true;
};
More... I think that you may not use both pure javascript and jquery
So you'd rather choose between
JAVASCRIPT :
document.getElementById('search_field').onkeypress = function(e) {
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13') {
window.location.href = '/search/?s=' + document.getElementById('search_field').value;
return false;
}
return true;
};
JQUERY
$( "#search_field" ).keypress(function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
window.location.href = '/search/?s=' + $(this).val();
return false;
}
return true;
});
End your function with return true.
If any other key then 13 is pressed the flow should just continue normally.
Ignore your tool. Event handlers do not have to return a value in every occasion, it is fine if only a particular path does return false.

Bind ctrl + u keystroke in jQuery

I am trying to build a hotkey into my web application in jQuery. I am trying to bind the Ctrl+U key stroke. Here is what I have:
$(document).keypress(function(e) {
if(e.ctrlKey && e.which == 117) {
if($("#nav-user-details").length > 0) {
$("#nav-user-details").find(".dropdown-menu").toggle();
}
}
});
This is not working though. How do I bind this key strokes?
Thanks.
Try this please http://jsfiddle.net/TN7GZ/
Press Ctrl+U and the screen will alert.
This will fit your need :)
Code
var isCtrl = false;
document.onkeyup=function(e){
if(e.which == 17) isCtrl=false;
}
document.onkeydown=function(e){
if(e.which == 17) isCtrl=true;
if(e.which == 85 && isCtrl == true) {
//run code for CTRL+U -- ie, whatever!
alert('CTRL + U stuff');
return false;
}
}
​
I’m pretty sure 85 is the keycode for u, or am I missing something?
If you want mac support as well (the command key), it can get messy. I wrote a snippet before that might help you, but it involves browser detections (yuck):
var cmd = false;
$(document).on('keydown', function(e) {
if(detectMacCommand(e.which)) {
cmd = true;
return;
}
// now detect print (ctr/cmd + p)
if ( e.which == 85 && ( e.ctrl || cmd ) ) {
e.preventDefault();
alert('ctrl/cmd + u');
}
}).on('keyup', function(e) {
if(detectMacCommand(e.which)) {
cmd = false;
return;
}
});
function detectMacCommand(key) {
return ( $.browser.mozilla && key == 224 ||
$.browser.opera && key == 17 ||
$.browser.webkit && ( key == 91 || key == 93 ));
}
Demo: http://jsbin.com/afijam/2
$(document).keypress("u",function(e) {
if(e.ctrlKey)
alert("Ctrl+U was pressed!!");
});
You'll have to use keydown instead of keypress. Keypress does not trigger for non-char keys.
$(document).keydown(function (e) {
if (e.ctrlKey && e.which === 81) {
alert("key pressed");
return false;
}
});
Try this:
var prevKey = null;
$(document).keydown(function (e) {
var thisKey = e.which;
if (prevKey && prevKey == 17) {
if (thisKey == 85) {
// Your code.
}
}
prevKey = thisKey;
});
If you are working in a Xhtml file and you get an error The entity must immediately follow the & then you should use &&& instead of &&.

Categories

Resources