Solved

Can someone tell me why the arrow keys not working?

Posted on 2011-09-29
15
167 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now