Solved

how to get the arrow left and right keys to fire off using jquery?

Posted on 2011-09-30
5
302 Views
Last Modified: 2012-06-27
The left and right arrow keys in the code below is not working.

<HTML>
<HEAD>
<TITLE>Layout Example</TITLE>
<SCRIPT type="text/javascript" src="jquery-1.6.2.min.js"></SCRIPT>
<script type="text/javascript">
$(document).ready(function()
{
      $('#container').click(function()
      {
            var totalWidth = 0;
            var sizeWidth = $('#insertData1').outerWidth();
            $('#ul_a li').each(function()
            {
                  var widthS = $(this).width();
                  var textW = $(this).text();
                 
                  var widthN = parseInt(widthS,10);
                  if((totalWidth + widthN) < sizeWidth)
                  {
                        totalWidth = totalWidth + widthN;
                        $('#ul_b').append('<li>'  + textW + '</li>');                              
                  }else
                  {
                      return false;                        
                  }
            });
                  $('#ul_b li').hover(function()
                  {
                            $(this).addClass('highlight');
                  }, function()
                  {
                            $(this).removeClass('highlight');
                  });
                  $('#ul_b li').keyup(function(e)
                  {
                        if(e.keyCode == 37)
                  {
                        $(this).removeClass('highlight');
                        $(this).prev().addClass('highlight');
                  }else if(e.keyCode == 39)
                  {
                        $(this).removeClass('highlight');
                        $(this).next().addClass('highlight');
                  }
                        return false;
                  });
            });
});
</script>
<style>
.highlight
{
      background-color: white;
      color: black;
}
#container
{
      height: 30px;
      width: 150px;
      background-color: black;
      color: white;
      text-align: center;
      position: absolute;
}
#verticalAlign
{
      height: 20px;
      position: absolute;
      top: 50%;
      margin-top: -10px;
      margin-left: 5px;      
}
ul
{
        list-style-type: none;
      margin: 0px;
      padding: 0px;
}
#ul_b li
{
       display: inline;
      margin: 0px;
      padding: 0px;
      text-align: center;
}
#insertData1
{
      width: 600px;
      height: 25px;
      background-color: black;
      color: white;
      margin-top: 20px;
      padding: 5px;
}
#ul_b
{
      list-style-type: none;
}
#ul_a li
{
      display: inline;
      visibility: hidden;
}
</style>
</HEAD>
<BODY>
<div id="container">
      <div id="verticalAlign"> Click Here </div>
</div>
<div>
      <ul id="ul_a">
                  <li>aaaa </li>
                  <li>aaaaaaaaa </li>
                  <li>aaaaaaaaa </li>
                  <li>aaaaaaaaa </li>
                  <li>aaaaaaaaa </li>
                  <li>aaaaaaaaa </li>
                  </ul>
<p></p>
</div>
<div id="insertData1"><ul id="ul_b"></ul></div>
</div>
</BODY>
</HTML>
0
Comment
Question by:areyouready344
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ncoo
ID: 36894199
The way you are using keyup will not work, take a look at the following example.

The left/right arrows return 0

Check out http://api.jquery.com/keypress/ for more details.
<HTML>
<HEAD>
<TITLE>Layout Example</TITLE>
<SCRIPT type="text/javascript" src="http://jquery.bassistance.de/jquery.js"></SCRIPT>
<script type="text/javascript">
$(document).ready(function()
{

$("#box").keypress(function(event) {
alert(event.which);
});

});
</script>
</HEAD>
<BODY>
<input type="text" id="box" /></BODY>
</HTML>

Open in new window

0
 

Author Comment

by:areyouready344
ID: 36894247
Thanks ncoo but how do I used in my situation. I have seen it worked in other situations but can not get it to work in my situation.
0
 

Author Comment

by:areyouready344
ID: 36894278
Not working after trying your suggestion of:

$('#ul_b li').keypress(function(e)
                  {
                        if(e.which == 37)
                  {
                        $(this).removeClass('highlight');
                        $(this).prev().addClass('highlight');
                  }else if(e.which == 39)
                  {
                        $(this).removeClass('highlight');
                        $(this).next().addClass('highlight');
                  }
                        return false;
                  });
0
 
LVL 15

Accepted Solution

by:
ncoo earned 500 total points
ID: 36895938
The first example work and picks up the left key
 
<html>
<head>
<script type="text/javascript" src="http://www.w3schools.com/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).keydown(function(e){
    if (e.keyCode == 37) {
       alert( "left pressed" );
       return false;
    }
});

</script>
</head>

<body>
<div id="content" style="width:200;height:200;border:1px solid red;">

</div>
</body>
</html>

Open in new window


The second example fails, it has no way to detect what element the keyup is for.
 
<html>
<head>
<script type="text/javascript" src="http://www.w3schools.com/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#content').keydown(function(e){
		if (e.keyCode == 37) {
		   alert( "left pressed" );
		   return false;
		}
	});
});

</script>
</head>

<body>
<div id="content" style="width:200;height:200;border:1px solid red;">

</div>
</body>
</html>

Open in new window


You need to think about what exactly you are trying to do with the navigation and if it can be done another way.
0
 

Author Comment

by:areyouready344
ID: 36896839
Thanks ncoo, see your point. I have to work through the logic
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Filling Out/Creating a PDF 29 102
jQuery on click requires double click 6 33
Getting selected value using Jquery 3 21
Echo'd values in dropdowns 6 30
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question