views:

128

answers:

1

I have a multi select list box. I want to be able to select a number of items and act on the selection by pressing the Enter key. The problem I'm having is that when I press Enter the current item at the cursor becomes the only item selected. The others are deselected so my function only acts on a single item. What can I do to prevent the Enter key from selecting an item?

I am setting up the onkeypress function in javascript as follows. The doListKeyPress checks for the enter key and acts on it.

select.onkeypress = function(e) { doListKeyPress(e, "myListName"); };
A: 

Add this event:

select.onkeydown = function(e) { if(e.keyCode == 13) select.blur(); };

As keydown comes before keypress, it will set the focus away from the listbox hence the default browser Enter behavior is 'bypassed'.

Disclaimer: This answer does not allow selection of item by Enter. It can be done with further implementation though.

o.k.w
Thanks for the pointer.The onkeydown function successfully prevents the Enter key from selecting a single option. However, since the select is blurred on the key down stroke the onkeypress event never gets fired, so I had to create a document.onkeypress function to handle it as follows. document.onkeypress = function(e) { if(e.keyCode == 13) { doListKeyPress(e, select); } };
@seppy, so you got what you want eventually?
o.k.w