Link to home
Start Free TrialLog in
Avatar of areyouready344
areyouready344Flag for United States of America

asked on

Can someone tell me why the arrow keys not working?

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>
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Can you please show your html code
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.
Avatar of areyouready344

ASKER

How do I fix this problem?
Move your arrow key handling code outside of the hover handler of "insertData2 li"
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
Avatar of anandblitz
anandblitz

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

37 - left

38 - up

39 - right

40 - down
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;
      });
});
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>
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...
Here is the link to the latest code

http://jsfiddle.net/nBsfm/14/
Still can not get it working....

http://jsfiddle.net/nBsfm/20/
Expert solution did not solve this problem
no thanks