Reputation: 45632
I have a stack of <DIV>
siblings and I want to let the user use the up and down arrows to move up and down the list and "highlight" an item. Only one item should be highlighted at any given moment.
What's the easiest way to do this?
Upvotes: 9
Views: 9187
Reputation: 42818
$(document).keyup(function(e) {
var $hlight = $('div.hlight'), $div = $('div');
if (e.keyCode == 40) {
$hlight.removeClass('hlight').next().addClass('hlight');
if ($hlight.next().length == 0) {
$div.eq(0).addClass('hlight')
}
} else if (e.keyCode === 38) {
$hlight.removeClass('hlight').prev().addClass('hlight');
if ($hlight.prev().length == 0) {
$div.eq(-1).addClass('hlight')
}
}
})
Upvotes: 13
Reputation:
Here's one way to do it without using IDs or classes. A working jsfiddle example is available here (make sure to click on the Result pane first).
The javascript:
var $currentDiv = $("#myContainer").children().first();
$currentDiv.css("background", "red");
$("html").keyup( function(keyEvent) {
if (keyEvent.keyCode == 40 ) {
var $nextDiv;
if ($currentDiv.next().size() == 0) {
$nextDiv = $("#myContainer").children().first();
}
else {
$nextDiv = $currentDiv.next();
}
$currentDiv.css("background", "");
$nextDiv.css("background", "red");
console.log($nextDiv);
console.log($currentDiv);
$currentDiv = $nextDiv;
}
else if (keyEvent.keyCode == 38) {
var $previousDiv;
if ($currentDiv.prev().size() == 0)
$previousDiv = $("#myContainer").children().last();
else {
$previousDiv = $currentDiv.prev();
}
$currentDiv.css("background", "");
$previousDiv.css("background", "red");
$currentDiv = $previousDiv;
}
});
The html:
<div id="myContainer">
<div> Div 1 </div>
<div> Div 2 </div>
<div> Div 3 </div>
<div> Div 4 </div>
</div>
Upvotes: 1
Reputation: 23263
OK...
var highlight = function(upOrDown){
var highlighted = $("#daddyDiv > div.higlighted");
if(highlighted[0] == null){
//If nothing is highlighted, highlight the first child
$("#daddyDiv > div:first").addClass("highlighted");
} else {
//Highlight the next thing
if(upOrDown == "down" && highlighted.index() != $("#daddyDiv > div").length()){
$("#daddyDiv > div").eq(highlighted.index()+1).addClass("highlighted");
$("#daddyDiv > div.higlighted").removeClass("highlighted");
} else if(upOrDown == "up" && highlighted.index() != 1){
$("#daddyDiv > div").eq(highlighted.index()-1).addClass("highlighted");
$("#daddyDiv > div.higlighted").removeClass("highlighted");
}
}
};
//Assuming you are using up/down buttons...
$("#upButton").click(function(){ highlight("up"); });
$("#downButton").click(function(){ highlight("down"); });
//Using the arrow keys...
$("body").keyup(function(e){
if(e.keyCode == "40"){
//Down key
highlight("down");
} else if(e.keyCode == "38"){
//Up key
highlight("down");
}
});
Upvotes: 2
Reputation: 154838
I made this up: http://jsfiddle.net/JPy76/.
It basically removes a highlighted class when moving up/down and adds it to the next/previous. Some extra code is necessary for moving down after the last one / moving up after the first one.
$('body').keydown(function(e) {
if(e.keyCode === 40) {
if($('.highlighted').next().length) {
$('.highlighted').removeClass('highlighted')
.next().addClass('highlighted');
}
else {
$('.highlighted').removeClass('highlighted');
var d = $('div');
d.length = 1;
d.addClass('highlighted');
}
}
if(e.keyCode === 38) {
if($('.highlighted').prev().length) {
$('.highlighted').removeClass('highlighted')
.prev().addClass('highlighted');
}
else {
$('.highlighted').removeClass('highlighted');
var d = $('div');
d = $(d[d.length - 1]);
d.addClass('highlighted');
}
}
});
Upvotes: 1