Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Can someone tell me why the arrow keys not working?

Posted on 2011-09-29
15
182 Views
Last Modified: 2012-05-12
This code this working except the left and right arrow keys. Can someone tell me why the arrow keys not working?

<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;
                        $('#insertData2').append('<li>'  + textW + '</li>');                              
                  }else
                  {
                      return false;
                       
                  }
                  $('#insertData2 li').hover(function()
                  {
                            $(this).addClass('highlight');
                        $('#insertData2 li').keyup(function(e)
                        {
                              if(e.keyCode == 37)
                              {
                                    $('#insertData2 li').addClass('highlight');
                              }else if(e.keyCode == 39)
                              {
                                    $('#insertData2 li').addClass('highlight');
                              }
                                    return false;
                        });
                  }, function()
                  {
                            $(this).removeClass('highlight');
                  });
            });
      });
                  
});
</script>
0
Comment
Question by:areyouready344
15 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36889927
Can you please show your html code
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36889948
you are assigning the keyup event to 'insertData2 li', only when they are hovered once.
So, if you haven't yet hovered on them yet, then it means the keyup event has not binded yet.
0
 

Author Comment

by:areyouready344
ID: 36890008
How do I fix this problem?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Expert Comment

by:yeelokhk
ID: 36890052
Move your arrow key handling code outside of the hover handler of "insertData2 li"
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36890092
copy this code
$('#insertData2 li').keyup(function(e)
                        {
                              if(e.keyCode == 37)
                              {
                                    $('#insertData2 li').addClass('highlight');
                              }else if(e.keyCode == 39)
                              {
                                    $('#insertData2 li').addClass('highlight');
                              }
                                    return false;
                        });

directly inside the document.ready, and outside $('#container').click
0
 

Expert Comment

by:anandblitz
ID: 36890169
$(document).keydown(function(e){
    if (e.keyCode == 37) {
       alert( "left pressed" );
       return false;
    }
});
-----------------------------------------------------------------------------
Character codes:

37 - left

38 - up

39 - right

40 - down
0
 

Author Comment

by:areyouready344
ID: 36891190
I put the keycode outside the container code as suggested but still not highlighting. The hover code is still working.

$(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;
                        $('#insertData2').append('<li>'  + textW + '</li>');                              
                  }else
                  {
                      return false;
                       
                  }
                  $('#insertData2 li').hover(function()
                  {
                            $(this).addClass('highlight');
                  }, function()
                  {
                            $(this).removeClass('highlight');
                  });
            });
      });
      $('#insertData2 li').keyup(function(e)
      {
            if(e.keyCode == 37)
            {
                  $('#insertData2 li').addClass('highlight');
            }else if(e.keyCode == 39)
            {
                  $('#insertData2 li').addClass('highlight');
            }
                  return false;
      });
});
0
 

Author Comment

by:areyouready344
ID: 36891497
Still does not work. I even tried putting it into a separate document.ready. The hover code is still working.

<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;
                        $('#insertData2').append('<li>'  + textW + '</li>');                              
                  }else
                  {
                      return false;
                       
                  }
                  $('#insertData2 li').hover(function()
                  {
                            $(this).addClass('highlight');
                  }, function()
                  {
                            $(this).removeClass('highlight');
                  });
            });
      });
});
</script>
<script type="text/javascript">
$(document).ready(function()
{
      $('#insertData2 li').keyup(function(e)
      {
            if(e.keyCode == 37)
            {
                 $('#insertData2 li').addClass('highlight');
            }else if(e.keyCode == 39)
            {
                  $('#insertData2 li').addClass('highlight');
            }
                  return false;
      });      
});
</script>
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 36891547
make it

 $('#insertData2 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;
      });
0
 

Author Comment

by:areyouready344
ID: 36891575
thanks gurvinder372 but still not working after trying:

 $('#insertData2 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;
      });

    Accept Multiple Solutions
    Accept as Solution

But the hover code is still working...
0
 

Author Comment

by:areyouready344
ID: 36891630
Here is the link to the latest code

http://jsfiddle.net/nBsfm/14/
0
 

Author Comment

by:areyouready344
ID: 36892312
Still can not get it working....

http://jsfiddle.net/nBsfm/20/
0
 

Author Comment

by:areyouready344
ID: 36893999
Expert solution did not solve this problem
0
 

Author Comment

by:areyouready344
ID: 36896831
no thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

789 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