I have created an ajax dropdown, i need to change the background color of the dropdown values when i press the down arrow and up arrow key.Here when i press the key background changes and it disappears immediately.it's not working in the ajax dropdown, if i put and alert before setting the class selectedhash, it's working else it's not working.
Here the div will be updated by the ajax results with list.
Please help me to solve this.
<div class='textautocomplete'>
</div>
$(document).on("keydown", function(e) {
if (e.keyCode == 40)
{
if(chosen === "")
{
chosen = 0;
} else if((chosen+1) < $('.textautocomplete ul').length)
{
chosen++;
}
$('.textautocomplete ul').removeClass('selectedhash');
$('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');
return false;
}
if (e.keyCode == 38) {
if(chosen === "") {
chosen = 0;
} else if(chosen > 0) {
chosen--;
}
$('.textautocomplete ul').removeClass('selectedhash');
$('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');
return false;
}
});
$(".textinput").live("keyup",function(e)
{
$.post('/users/getusers',{data:dataString},function(result){
if(result!=='')
{
$('.textautocomplete').show();
$('.textautocomplete').html(result);
}
else
{
$('.textautocomplete').hide();
$('.textautocomplete').html('');
}
});
return false
});
Firstly change: ".live" to: ".on" in line:
$(".textinput").live("keyup",function(e)
because:
"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()."
More: jQuery .live() method
Secondly add keyCode filter code in:
$(".textinput")
For example:
if ( e . keyCode == 38 || e . keyCode == 40 ) { return false; }
Working example fiddle: JSFiddle
Related
I know this question has been asked here. Basically the solution works but they are using an old version of jquery and the solution was implemented using deprecated functions. So I tried to migrate to the new version, according to answers and questions from this question.
So basically the solution using the old jquery version is this one.
$('textarea.paginate').live('keydown', function(event) {
if (this.clientHeight < this.scrollHeight) {
alert("Please Something);
}
});
This was my migration to the on function, but neither way works. Demo of the example: http://jsbin.com/saxay/1/edit
What I'm doing wrong?
$("textarea").on('keyup','.note-codable',function(event){
if(event.keyCode == 13) { }
if (this.clientHeight < this.scrollHeight) {
alert("Please Something");
}
});
$(document).on('keyup','.note-codable',function(event){
if(event.keyCode == 13) { }
if (this.clientHeight < this.scrollHeight) {
alert("Please Something");
}
});
Fixed your problem
You just had to use .on at the place of live in your first approach.
$('textarea.paginate').on('keydown', function(event) {
// scrollbars apreared
if (this.clientHeight < this.scrollHeight) {
alert("Please add a new card for having a better format. Remember this is a WYSIWYG");
}
});
DEMO
UPDATED
try this approach: DEMO
$.fn.hasVerticalScrollBar = function() {
if (this[0].clientHeight < this[0].scrollHeight) {
return true
} else {
return false
}
};
$(document).on('keydown','.note-codable',function(event){
if(event.keyCode == 13) { }
if ($(this).hasVerticalScrollBar()) {
alert("Please Something");
}
});
also if you notice I've changed the keyup event to keydown which is better in my opinion 'cause when the user holds their finger down on a button the code wouldn't be fired if it is on the keyup event.
I am using the JQuery UI Dialog to create a pop-up. The pop-up has two buttons. The first button is automaticly selected by JQuery. I can change the selection between the buttons and the exit button with 'tab'.
I want to change the selection (only between the two buttons) also with the left and right arrow keys on the keyboard.
Where do I have to catch the arrow key down events and how can I change the focus of the buttons?
Thanks for your help!
I would do it something like that :)
$('body').on('keydown', '#terug', function(e) {
if (e.keyCode === 39) { //right arrow
$('#ok').focus();
}
});
$('body').on('keydown', '#ok', function(e) {
if (e.keyCode === 37) { //left arrow
$('#terug').focus();
}
});
Try it :) And if this won't work then go global without specifying selector in event definition:
$('body').on('keydown', function(e) {
if (e.keyCode === 39 && $('#terug').is(':focus')) { //right arrow
$('#ok').focus();
}
});
Hope this will help! :) If not give me a comment and i will try to fix this. :)
Thanks for your help! It worked. I added my solution to complete this question.
I bind the keydown event only on the ui-buttons:
$(document).on('keydown', '.ui-button', handleUIButtonKeyDown);
After that I handle the left and right arrow keys
function handleUIButtonKeyDown(event) {
if (event.keyCode == 37) {
//left arrow key pressed, select the previous button
makeButtonFocus($(this).prev(".ui-button"));
} else if (event.keyCode == 39) {
//right arrow key pressed, select the next button
makeButtonFocus($(this).next(".ui-button"));
}
}
function makeButtonFocus($button) {
$button.addClass("ui-state-focus");
$button.focus();
}
Here's a more generic answer
works on any number of buttons irregardless of DOM structure (btns need not be siblings)
$('body').on('keydown', function(e) {
if (e.keyCode === 37) { //left arrow
modalKeyboardNav("prev")
} else if (e.keyCode === 39) { //right arrow
modalKeyboardNav("next");
}
});
function modalKeyboardNav(dir) {
if (!$("body").hasClass("modal-open")) {
// no modal open
return;
}
var $curModal = $(".modal.show"),
$curFocus = $(document.activeElement),
$focusable = $curModal.find(".btn"),
curFocusIdx = $focusable.index($curFocus);
if (curFocusIdx < 0) {
// nothing currently focused
// "next" will focus first $focusable, "prev" will focus last $focusable
curFocusIdx = dir == "next" ? -1 : 0;
}
if (dir == "prev") {
// eq() accepts negative index
$focusable.eq(curFocusIdx - 1).focus();
} else {
if (curFocusIdx == $focusable.length - 1) {
// last btn is focused, wrap back to first
$focusable.eq(0).focus();
} else {
$focusable.eq(curFocusIdx + 1).focus();
}
}
}
I'm trying to turn a button-click into a toggle that enables or disables a function, depending on its state. The function allows the enter key to be used for a form submission.
var enterToggle = true;
function enterToggleListener(elem) {
enterKeyPress();
elem.click(function() {
enterToggle = !enterToggle;
console.log('enter-toggle clicked')
if (enterToggle === false) {
console.log('enter toggle false')
// What do I need to add here to stop 'enterKeyPress()'?
} else {
console.log('enter toggle true')
enterKeyPress();
}
});
}
function enterKeyPress() {
$('#noteText').keypress(function(e){
if(e.which == 13){
$('#noteButton').click();
}
});
}
enterToggleListener($('#toggle-button'));
What I don't understand is how to stop the enterKeyPress() function when enterToggle is false. Any suggestions?
EDIT: Cleaned-up code, with #James Montagne's answer added
var enterToggle = true;
function enterToggleListener(elem) {
elem.click(function() {
enterToggle = !enterToggle;
if (enterToggle === false) {
$('#enter-toggle').text('Enter key saves note (OFF)')
} else {
$('#enter-toggle').text('Enter key saves note (ON)')
}
});
}
function enterKeyPress() {
$('#noteText').keypress(function(e){
if(enterToggle && e.which == 13){
$('#noteButton').click();
}
});
}
enterKeyPress();
enterToggleListener($('#enter-toggle'));
function enterKeyPress() {
$('#noteText').keypress(function(e){
if(enterToggle && e.which == 13){
$('#noteButton').click();
}
});
}
You can simply check the value of the variable within your handler. This way you don't need to keep adding and removing the handler as seems to be your current approach.
However, if you must add and remove for some reason, you would use off.
Simply I have a js script that change the page with left and right arrows, but how to stop that if a specific textarea is selected ?
This is my js to change the page
$(document).keydown(function(event) {
if(event.keyCode === 37) {
window.location = "http://site.com/pics/5";
}
else if(event.keyCode === 39) {
window.location = "http://site.com/pics/7";
}
});
$('textarea').on('keypress', function(evt) {
if ((evt.keyCode === 37) || (evt.keyCode === 39)) {
console.log('stop propagation');
evt.stopPropagation();
}
});
See example fiddle: http://jsfiddle.net/GUDqV/1
Update: after OP clarification this works even on jQuery 1.2.6 on Chrome: http://jsfiddle.net/GUDqV/2/
$('textarea').bind('keyup', function(evt) {
if ((evt.keyCode === 37) || (evt.keyCode === 39)) {
console.log('stop propagation');
evt.stopPropagation();
}
});
see screenshot of this code on Chrome and jQ1.2.6
Probably the simplest approach is to factor event.target into your code, checking to see if it is the textarea:
$(document).keydown(function(event) {
if (event.target.id == "myTextArea") {
return true;
}
else if(event.keyCode === 37) {
window.location = "http://site.com/pics/5";
}
else if(event.keyCode === 39) {
window.location = "http://site.com/pics/7";
}
});
Any key events that originate from a textarea element with an id of myTextArea will then be ignored.
You can check if the textarea is in focus by doing something like:
if (document.activeElement == myTextArea) {
// Don't change the page
}
$("#mytextarea").is(":focus") This will let you know if the element is focused.
Also $(document.activeElement) will get the currently focused element.
You can check to see if your text area is focused, and disable the script that navigates when using left and right arrow keys.
A little bit of code showing what you've tried might bring in more specific responses.
Hope this helps.
I wrote the following jquery code to allow only numbers in a textbox.
$("[id$='txtPriority']").keydown(function (event) {
if (event.keyCode == 48) {
var value = $("[id$='txtPriority']").val();
if (value == "") {
event.preventDefault();
return;
}
}
else if (event.keyCode == 86 || event.keyCode == 118) {
event.preventDefault();
return;
}
else {
$("[id$='txtPriority']").numeric();
}
});
});
This works fine ,when the page is loaded for first time.But after post back the code is not working.What might be the reason.
I you are changing something with ajax then the event will not work.
Try using the live function ( http://api.jquery.com/live/ )
$("[id$='txtPriority']").live('keydown', function (event) {
I had this problem once and it was because I was replacing the html for the form in the postback. You need to reset your event handlers after replacing the controls or use the .live() handler which will work even for elements you add later:
$("[id$='txtPriority']").live('keydown', function (event ) {