Basically I have this select list with multiple options in it:
<select name="drop1" id="Select1" size="30">
<?php
for($i = 0;$i<30;$i++)
{
echo "<option id=\"other\" value=\"" . $i . "\"></option>\n";
}
?>
</select>
I also have an input field:
<input type="text" id="inputfield"></input>
With jquery I want to accomplish the following: Whenever the inputfield is focussed and the arrow up is pressed, I want to move 1 option up in the select list. Same thing with arrow down. I already found out what jquery events to use to know when key down and up are pressed:
$('#inputfield').keydown(function(e){ //When a key is pressed..
var code = (e.keyCode ? e.keyCode : e.which);
if ((e.keyCode || e.which) == 40) // on key down
{
// Make selection go down here
}
if((e.keyCode || e.which) == 38) // on key up
{
// Make selection go up here
}
});
I’d suggest the following:
JS Fiddle demo.
Updated in response to comment left by the OP (below):
In order to trigger the
changeevent, simply usetrigger(); also, in the following code, I’ve added sanity checks to try and ensure that the key-up/key-down will only happen, and therefore only trigger achangeevent, when theselectoption’s selectedoptioncan actually change:JS Fiddle demo.