jquery elastic textarea issue - javascript

i am using this plugin :http://www.unwrongest.com/projects/elastic/ and this is my code :
$(document).ready(function () {
$("#<%=Message_txt.ClientID %>").elastic();
var keyPressCount = 0;
$(window).keyup(function (e) {
if (keyPressCount++ % 10 == 0) {
chat.server.onuserTyping(ToClient, fromClient);
}
var enterkey = e.which == 13 || e.KeyCode == 13 || e.charCode == 13 ? true : false;
if (enterkey) {
var Msgtxt = $("#<%=Message_txt.ClientID %>").val().trim();
if (Msgtxt == null) return false;
$("#<%=Send_btn.ClientID %>").trigger("click");
}
});
});
as you can see when the enter key is pressed i am triggering the send_btn click the problem is after the button is triggered the textarea loose the elastic functionality and i don't know why

Related

Restriciting enterting of alphabets in jquery mobile

I am using a text box with type number. For xperia mobile's if i am using the same text box,it is showing a keyboard with all the value.So i am able to type any value. I want to restrict the user from entering any value.User have to enter only number.I tried the following code.
*
document.getElementById('loginPin').addEventListener('keyup', function(e){
var keyCode = ('which' in e) ? e.which : e.keyCode;
var isNumeric = (keyCode < 48 KeyboardEvent.DOM_VK_0 && keyCode > 57 KeyboardEvent.DOM_VK_9 ) ||
(keyCode < 96 KeyboardEvent.DOM_VK_NUMPAD0 && keyCode > 105 KeyboardEvent.DOM_VK_NUMPAD9 );
var modifiers = (event.altKey || event.ctrlKey || event.shiftKey);
return !isNumeric || modifiers;
var V = $(this).val();
alert("hai"+V);
if (isNaN(V)) {
$(this).val(V.replace(/[^0-9]/g,''));
}
}, false);
*
It is working if the alert is there, else it is not working. So any suggestion to solve this.
function numericInput(inputId){
$('#' + inputId).keyup(function (e) {
var regex = new RegExp("^[0-9]+$");
if (!regex.test($(this).val())) {
$(this).val($(this).val().slice(0, -1));
e.preventDefault();
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

page is going up on pressing tab key

I created a function using jquery tabs. The function working fine. but when I press tab key the Page is moving up.
What am I doing wrong here?
$(document).on('keydown', function (e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 9) {
if (!$('#appinfo:last-child').is(':focus') && window.location.href.search('appinfo') != -1) {
$('#ATab1').removeClass('active');
$('#ATab2').addClass('active');
$('#appinfo').hide();
$('#loanprogram').show();
$('#miworksheet').hide();
window.location = '#loanprogram';
}
else if (!$('#loanprogram:last-child').is(':focus') && window.location.href.search('loanprogram') != -1) {
$('#ATab2').removeClass('active');
$('#ATab3').addClass('active');
$('#appinfo').hide();
$('#loanprogram').hide();
$('#miworksheet').show();
window.location = '#miworksheet';
}
}
});

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 &&.

Event is not defined error in javascript only Firefox

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..

Categories

Resources