Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 08:11 AM   PM User | #1
MikeW88
New to the CF scene

 
Join Date: Apr 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
MikeW88 is an unknown quantity at this point
Problem adding arrow key navigation to Jquery AJAX + PHP search engine

I've made a search engine for my database table where the search query gets sent by Jquery AJAX to a file called search.php. Search.php then sends the result back to the Javascript file where the results are processed and added to index.php.

Everything works perfectly except when I try to add arrow key navigation. For example, I want the first item in the search results page to be selected ( by appending the class, "red" to it and using "focus()" ) when I press the down arrow key, the second result to be selected when I press it again, etc..

Nothing gets selected when I press either the up or down arrow key. I'm testing this in Google Chrome.

index.php:

PHP Code:
<style>

#search_results{
border:1px solid blue;
width:300px;
position:absolute;
background:white;
display:none;

}
#search_results ul li{
    
}
#search{
width:300px;
height:30px;    
}
.
red{
background:red;    
}

</
style>

Start searching: <input type="text" id="search" autocomplete="off">
Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
<div id="search_results">
</
div>

<
script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search.js"></script> 
search.js:

Code:
$(document).ready(function(){

search_x = $('#search').offset().left;
search_y = $('#search').offset().top;
search_height = $('#search').height();
$('#search_results').css({
	'left': search_x,
	'top': (search_y + search_height + 5)
});

//arrow key navigation
start = -1;
$(document).keydown(function(e) {    
	
	if(e.keyCode == 38){
		
	if (start == -1){
		start = ($('#search_results ul li a').size() - 1);	
	}else{
		start--;
		if (start < 0){
			start = ($('#search_results ul li a').size() - 1);	
		}
	}
	$('#search_results ul li a').removeClass('red').focus();
    $('#search_results ul li a').eq(start).addClass('red').focus();
	
	}

    if(e.keyCode == 40){

    if (start == -1){
		start = 0;	
	}else{
		start++;
		if (start > ($('#search_results ul li a').size() - 1)){
			start = 0;	
		}
	}
    $('#search_results ul li a').removeClass('red').focus();
    $('#search_results ul li a').eq(start).addClass('red').focus();
    
 
    }
});




$('#search').keyup(
function(){
	var search_term = $(this).val();
	
	$.post(
	'search.php',
	{
	search_term : search_term		
		
	},function(data){
		if (data == "nothing"){
			$('#search_results').fadeOut();
		} else {
		
					$('#search_results').html(data).fadeIn();

		
		
		}
	});	
});

});

$('body').click(function(){
	$('#search_results').fadeOut();
	
});

Last edited by MikeW88; 05-01-2012 at 05:18 AM..
MikeW88 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.