1

I have a listbox, with items that can be in some kind of group with other items. When one item is selected, all items from the group are selected with jquery.

Its really annoying that the listbox scrolls down to the items that jquery is selecting. I want to stay at the position of the item selected by the user.

So how can i prevent the listbox from scrolling down when items get selected?

jsfiddle example here: example

EDIT: click on item number 10 in the example, and he goes to 78, thats the issue here.

Erik Dekker
  • 2,395
  • 5
  • 33
  • 55

1 Answers1

0

You can try this:

$('#lb').change(function() { 

    var x = $(this).scrollTop();

    $('#lb option:selected').each(function() { 
        var groupName = $(this).attr('group'); 
        if (groupName !== undefined) { 
            $('#lb option[group=' + groupName + ']').each(function() { 
                this.selected = true; 
            }); 
        }

    }); 

     $(this).scrollTop(x); 
} );
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • It does do 'something': it always scrolls to the top of the listbox, but it looks like the option mousedown is not working, since it always going to the top now. and the } before $('option') should be a }); – Erik Dekker Aug 30 '11 at 15:38
  • Editted answer... it now stores the scrolling before updating. – Kees C. Bakker Aug 31 '11 at 08:17
  • Works great, only little thing is that if you start to scroll after selecting nr 10, it starts scrolling at 78. But its much better this way! – Erik Dekker Aug 31 '11 at 08:27