?
Solved

Can someone tell me why the arrow keys not working?

Posted on 2011-09-29
15
Medium Priority
?
187 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
[X]
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
15 Comments
 
LVL 17

Expert Comment

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

Expert Comment

by:Gurvinder Pal Singh
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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:Gurvinder Pal Singh
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:
Gurvinder Pal Singh earned 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

765 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