...

View Full Version : Left and Right Arrow keys assigned onkeydown help



mpal
12-10-2010, 07:31 PM
I am trying to assign the left and right arrows, but I cannot get the code to work. It would be great to get some help--I am a newbie to coding.

<html>
<head>
<script language="javascript" type="text/javascript">
document.onkeydown=function(e) {
var thelink
if (e.keyCode==37) thelink='prevlink'
else if (e.keyCode==39) thelink='nextlink'
else return
document.location.href=document.getElementById(thelink).href}
</script>
</head>
<body>
<a href="#" onkeydown="return previous(this)" id="prevlink">PREVIOUS</a><br />
<br />

<a href="#" onkeydown="return next(this)" id="nextlink">NEXT</a>

</body>
</html>

Kor
12-11-2010, 09:32 AM
Whenever you say "I can not get this code to work" you should be more specific. What does not work? In which browser? What happens? What error do you get, if any?

Anyway, this one works:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
document.onkeydown=function(e){
var e=e||window.event; // Moz model || IE model
var id;
if(e.keyCode==37){id='prevlink'}
else if(e.keyCode==39){id='nextlink'}
else{return}
location.href=document.getElementById(id).href;
}
</script>
</head>
<body>
<a href="http://www.google.com" id="prevlink">Google</a>
<br>
<br>
<a href="http://www.yahoo.com" id="nextlink">Yahoo</a>
</body>
</html>


There are two models of capturing the events: Mozilla (inherit from old Netscape) and IE. And location is a property of the window Global Object (which usually is omitted in the code, just because it is the ultimate Object), not of the document object



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum