areyouready344
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(func tion()
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid th();
$('#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('highligh t');
$('#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('highl ight');
});
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function
{
$('#container').click(func
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid
$('#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('
}else
{
return false;
}
$('#insertData2 li').hover(function()
{
$(this).addClass('highligh
$('#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('highl
});
});
});
});
</script>
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.
So, if you haven't yet hovered on them yet, then it means the keyup event has not binded yet.
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
$('#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
$(document).keydown(functi on(e){
if (e.keyCode == 37) {
alert( "left pressed" );
return false;
}
});
-------------------------- ---------- ---------- ---------- ---------- ---------- -
Character codes:
37 - left
38 - up
39 - right
40 - down
if (e.keyCode == 37) {
alert( "left pressed" );
return false;
}
});
--------------------------
Character codes:
37 - left
38 - up
39 - right
40 - down
ASKER
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(func tion()
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid th();
$('#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('highligh t');
}, function()
{
$(this).removeClass('highl ight');
});
});
});
$('#insertData2 li').keyup(function(e)
{
if(e.keyCode == 37)
{
$('#insertData2 li').addClass('highlight') ;
}else if(e.keyCode == 39)
{
$('#insertData2 li').addClass('highlight') ;
}
return false;
});
});
$(document).ready(function
{
$('#container').click(func
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid
$('#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('
}else
{
return false;
}
$('#insertData2 li').hover(function()
{
$(this).addClass('highligh
}, function()
{
$(this).removeClass('highl
});
});
});
$('#insertData2 li').keyup(function(e)
{
if(e.keyCode == 37)
{
$('#insertData2 li').addClass('highlight')
}else if(e.keyCode == 39)
{
$('#insertData2 li').addClass('highlight')
}
return false;
});
});
ASKER
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(func tion()
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid th();
$('#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('highligh t');
}, function()
{
$(this).removeClass('highl ight');
});
});
});
});
</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>
<script type="text/javascript">
$(document).ready(function
{
$('#container').click(func
{
var totalWidth = 0;
var sizeWidth = $('#insertData1').outerWid
$('#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('
}else
{
return false;
}
$('#insertData2 li').hover(function()
{
$(this).addClass('highligh
}, function()
{
$(this).removeClass('highl
});
});
});
});
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks gurvinder372 but still not working after trying:
$('#insertData2 li').keyup(function(e)
{
if(e.keyCode == 37)
{
$(this).removeClass('highl ight');
$(this).prev().addClass('h ighlight') ;
}else if(e.keyCode == 39)
{
$(this).removeClass('highl ight');
$(this).next().addClass('h ighlight') ;
}
return false;
});
Accept Multiple Solutions
Accept as Solution
But the hover code is still working...
$('#insertData2 li').keyup(function(e)
{
if(e.keyCode == 37)
{
$(this).removeClass('highl
$(this).prev().addClass('h
}else if(e.keyCode == 39)
{
$(this).removeClass('highl
$(this).next().addClass('h
}
return false;
});
Accept Multiple Solutions
Accept as Solution
But the hover code is still working...
ASKER
ASKER
ASKER
Expert solution did not solve this problem
ASKER
no thanks