Select items in a list with keyup JavaScript, JQuery
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to make a list to select items but the behavior when I use up or down arrow is not so good. When I use up or down arrows it scrolls the <div>
and also select and focus on <li>
tag but the scrolls moves first so it seems to be kind of rare.
How can I achieve the correct behavior using up and down key?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really appreciate your help guys.
javascript jquery
add a comment |
I'm trying to make a list to select items but the behavior when I use up or down arrow is not so good. When I use up or down arrows it scrolls the <div>
and also select and focus on <li>
tag but the scrolls moves first so it seems to be kind of rare.
How can I achieve the correct behavior using up and down key?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really appreciate your help guys.
javascript jquery
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54
add a comment |
I'm trying to make a list to select items but the behavior when I use up or down arrow is not so good. When I use up or down arrows it scrolls the <div>
and also select and focus on <li>
tag but the scrolls moves first so it seems to be kind of rare.
How can I achieve the correct behavior using up and down key?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really appreciate your help guys.
javascript jquery
I'm trying to make a list to select items but the behavior when I use up or down arrow is not so good. When I use up or down arrows it scrolls the <div>
and also select and focus on <li>
tag but the scrolls moves first so it seems to be kind of rare.
How can I achieve the correct behavior using up and down key?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really appreciate your help guys.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style>
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
</style>
<script>
$(function(){
num = ""
$("body").keyup(function(e){
if (e.keyCode == 40){
$("ul").find('li:eq('+num+')').focus().css('color', 'red');
console.log(num ++)
}
if (e.keyCode == 38){
$("ul").find('li:eq('+num+')').focus().css('color', 'blue');
console.log(num --)
}
})
})
</script>
</head>
<body>
<ul>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
javascript jquery
javascript jquery
edited Nov 23 '18 at 20:55
Scott Marcus
41k52141
41k52141
asked Nov 23 '18 at 20:41
El arquitectoEl arquitecto
102110
102110
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54
add a comment |
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54
add a comment |
1 Answer
1
active
oldest
votes
I do not understand your problem exactly, but I will try to help you.
For this solution that I have prepared for you.
Let me know if you did not mean it!
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452726%2fselect-items-in-a-list-with-keyup-javascript-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I do not understand your problem exactly, but I will try to help you.
For this solution that I have prepared for you.
Let me know if you did not mean it!
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
add a comment |
I do not understand your problem exactly, but I will try to help you.
For this solution that I have prepared for you.
Let me know if you did not mean it!
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
add a comment |
I do not understand your problem exactly, but I will try to help you.
For this solution that I have prepared for you.
Let me know if you did not mean it!
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
I do not understand your problem exactly, but I will try to help you.
For this solution that I have prepared for you.
Let me know if you did not mean it!
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
$(function() {
var num = 0;
$("body").keyup(function(e) {
if (e.keyCode == 40) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'red');
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex + 1));
$("ul > li").eq(elementIndex + 1).focus();
}
if (e.keyCode == 38) {
var elementIndex = $("li:focus").index();
$("ul > li").eq(elementIndex).css('color', 'blue');
if(elementIndex == 0) return;
console.log("Last focused element index: " + elementIndex + " has change to: " + Math.floor(elementIndex - 1));
$("ul > li").eq(elementIndex - 1).focus();
}
})
})
ul {
width: 400px;
height: 200px;
background: aqua;
overflow: scroll;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<ul>
<li tabindex="0">Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
<li tabindex="0" >Palabra Palabra Palabra Palabra</li>
</ul>
</body>
</html>
answered Nov 23 '18 at 21:19
Maciej WojtasikMaciej Wojtasik
4615
4615
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
add a comment |
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
I really apreciate your help Maciej Wojtasik, no, what I mean just look the behavior of the scroll bar when you type up or down, look this site kwfinder.com and try to chose a language, just move the up an down key arrows and see how the scroll bar moves, and se my example and your example, thats the problem and I do not know how to achieve this.
– El arquitecto
Nov 23 '18 at 22:31
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
You were right ! Thank you !
– El arquitecto
Nov 24 '18 at 2:10
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
@Elarquitecto I also found an interesting solution on this page: w3.org/TR/2017/NOTE-wai-aria-practices-1.1-20171214/examples/… Maybe it will be useful to you!
– Maciej Wojtasik
Nov 24 '18 at 15:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452726%2fselect-items-in-a-list-with-keyup-javascript-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please clarify what you think the problem is and what the desired behavior should be.
– Scott Marcus
Nov 23 '18 at 20:54