How do I get the last thing input from an "input" event - javascript

I have been trying to deal with getting the last input in to a text field on an Android app from Javascript.
I was originally trying to use the KeyUp, KeyDown and KeyPress events to get the KeyCodes from keyboard input however with android soft keyboards you will get the KeyCode 229 for every key other than backspace, space and enter.
So now I am using the onInput event however this just seems to alert me to the fact that something has been input in to a text field and not what that input was.
document.getElementById("textarea").addEventListener("input", function (e) {
ClaroSpeak.KeyHandler(e);
}, false);
Does the input event some how let me know what the last change or key was, e.g. 'space' 'a' 'F' ect...

What about setting a 'global' variable and storing the last element in it?

Related

Detect entered character with jquery(spanish,áúí)

I'm working on #mention feature and need to load list of users that start with entered character in an input text field .
The problem is with Spanish character like (íáúóé).
in my code I use keypress and keydown to detect characters but in Spanish the keypress event doesn't fire .
in order to type the "á" user need to press two keys
first one is "´" and second is "a" ,then we got "á".
in keydown event using the method String.fromCharCode(e.which || e.keyCode) on this key"´" return "å"
The question how to catch this scenario?
Thanks

Detecting Special Key Presses in Javascript

I have an input box and want to adjust the value when a key is pressed.
'keypress .comment-input': 'onCommentInputKeyBlur',
When a key is pressed, a class is added to the html element to reflect the changes.
onCommentInputKeyBlur: function(ev) {
var $form = $('#comment-submit');
if (ev.which) {
$form.addClass('focused');
} else if (!$(ev.currentTarget).val()) {
$form.removeClass('focused');
}
},
However, this doesn't detect special keys being pressed (ie when a user presses ctrl+v for a paste, it's not detected and the formatting is therefore wrong).
Using keyup and keydown halfway solve the problem, but formatting is wrong for a brief second:
Keydown -> the value isn't supposed to change until an actual value is entered through a keypress, but it changes right when ctrl is hit instead of waiting for the second key
Keyup -> new value is briefly pasted over the previous one while waiting for the key to actually be released.
Is there a better way to about solving this? Ideally, I would like to detect if the key entered does actually produce a value and is not simply a special key.
Look into the onchange event. It is fairly well supported and may solve your problem.
What is the ultimate goal of this functionality? perhaps there is a better approach.
If you insist on doing this via keypress event, then you can simply check event.keyCode and filter out the few keycodes that dont change anything, such as shift, control, alt.

how to distinguish which 'Enter' key was pressed?

So I've got this textarea which listens for Enter keypress event. But I only want it to respond to the Enter key being pressed on the number pad not the one next to the letters.
What's a method I can use?
Both keys trigger the exactly same events with the same key code (13). It's not possible to differ between them in JavaScript.
There is no different keyCode for nomal enter and enter on keypad. Its 13 in both cases
http://unixpapa.com/js/key.html
They actually do the same thing and are indistinguishable

JQuery cancel entered text

When typing into a <input type="text"> field I would like to precheck every entered value before it appears on the screen. E.g. if the user enters any non numeric value, nothing will happen and only if he enters a numeric value the field will change.
Which is the right event to use keydown(), keyup() or is there something better? How do I cancel the current change of the text field without having to remember the old value and manually resetting it?
You could bind to the keypress event and then check the character represented by the which property of the event object. If it isn't a number you can use preventDefault to prevent the default behaviour of writing the character to the element:
$("#someInput").keypress(function(e) {
if(isNaN(String.fromCharCode(e.which))) {
e.preventDefault();
}
});
Here's a working example.

how to check if the value of a text input is empty right after the keypress event in javascript?

Here's the problem, in abstract terms: i have three input fields (A, B, C). two of them need to be text inputs (A and B), the third is of irrelevant type. I need to enable the third if A is not empty and B is not empty. I need to disable C if A is empty or B is empty.
The code
// empty is the empty function from the phpjs project
// framework used: jQuery
// A, B and C are classes here
$(".A, .B").keypress(function(){
if( !empty($(".A").val()) && !empty($(".B").val()) )
$(".C").attr("disabled","");
else
$(".C").removeAttr("disabled");
});
I want to be able to check this on keypress, but when requesting the value of the input that is edited when the keypress event occurs i get the value that was calculated before the keypress event.
Has anybody stumbled upon this before and solved it?
have you tried using the keyUp event?
Use the keyup event instead.
Attach your handler to the keyrelease event. The value should have been updated by then.
use a combination of handlers for keyup and change. the keyup handler will update as the user types (excepting edge cases like holding a key down, which doesn't seem like a concern here) and the change handler will catch things like the user cutting the text with mouse actions before they can switch to field C. as an added measure you could add verification on field C's focus event, to make sure A and B really have something.
$('.A, .B').keydown(function(event) {
if(!empty($('.A').val()) && !empty($('.B').val()))
$(".C").attr("disabled","");
else
$(".C").removeAttr("disabled");
});
At the keypress event, the value of the INPUT is not yet set. Hence you can't easily know if it is empty.
While the keyup fires, the value of the INPUT is set.
But things get worse. The user can empty the field with the mouse or the Edit menu of the browser. In that case keyup is not fired.
In our web app we auto-save the values. Like in Mac OS, if you know, there is almost no save buttons.
To allow a reaction here is more or less what we do:
onfocus: a setInterval starts polling every 120ms or so, and checks the input for any change
if there is a change do the relevant action
onblur: a clearInterval stop the polling

Categories

Resources