Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

select li element with arrow keys (up and down) using javascript

var li = $('#list > li');
var liSelected;
$(window).on('keydown', function(e){
var selected;
if(e.which === 40){
	if(liSelected){
		liSelected.removeClass('background');
		next = liSelected.next();
		if(next.length > 0){
			liSelected = next.addClass('background');
			selected = next.text();

		}else{
			liSelected = li.eq(0).addClass('background');
			selected = li.eq(0).text();
		}
	}else{
		liSelected = li.eq(0).addClass('background');
			selected = li.eq(0).text();
	}
}else if(e.which === 38){
	if(liSelected){
		liSelected.removeClass('background');
		next = liSelected.prev();
		if(next.length > 0){
			liSelected = next.addClass('background');
			selected = next.text();

		}else{

			liSelected = li.last().addClass('background');
			selected = li.last().text()
		}
	}else{

		liSelected = li.last().addClass('background');
		selected = li.last().text()
	}
}
console.log(selected)
});

/*HTML FILE CODE

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<link rel="stylesheet" href="">
	<style>
		.background{
			background: hsla(0, 100%, 0%, 0.4);
		}
	</style>
</head>
<body>
	<input type="text" class="form-control" id="searchProduct" placeholder="Search..." />
	<ul id="list">
    <li id="match1" class="itemList">1</li>
    <li id="match2" class="itemList">2</li>
    <li id="match3" class="itemList">3</li>
	</ul>
</body>
</html>





*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: hcaptcha bypass 
Javascript :: sentry ignoreerrors 
Javascript :: string interpolation in javascript 
Javascript :: vue js routue push 
Javascript :: javascript current target 
Javascript :: i want to redirect to main page from iframe javascript 
Javascript :: react build command 
Javascript :: npm config proxy 
Javascript :: unidirectional data flow react 
Javascript :: how to change text color sweet alert IN JS 
Javascript :: react router route not found redirect 
Javascript :: react native navigation remove top header screen 
Javascript :: web3 js get network 
Javascript :: print object keys 
Javascript :: javascript object instead of switch 
Javascript :: js object contains key 
Javascript :: randint js 
Javascript :: how to get date in footer javascript 
Javascript :: vue access computed property in data 
Javascript :: javascript random int 
Javascript :: how to write a json in r 
Javascript :: jquery with npm in laravel 
Javascript :: Lodash.chunk chunk 
Javascript :: drupal8 get params from route 
Javascript :: match city regex 
Javascript :: vscode shortcut to search for file 
Javascript :: use these instead of a for loop javascript 
Javascript :: console javascript 
Javascript :: js log stack trace 
Javascript :: print page using js 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =