Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery, how to scroll the selected option into visible view?

Tags:

jquery

Here I created a sample to help me be understood, http://www.jsfiddle.net/BLvsF/

I want the item-'a6' to be scrolled into the visible select box, how can I make it? I'd like to use jquery to do same as

$(document).ready(function() {
    $('#btn').click(function() {
        document.getElementById('a6').scrollIntoView();
        });
});

but, how to implement the same using jQuery?

I tried using .get(0).scrollIntoView(). but still not applicable.

$(document).ready(function() {
    $('#btn').click(function() {
        $('#a> option:selected').clone(false).appendTo('#b').get(0).scrollIntoView();
    });
});

http://www.jsfiddle.net/CYQfD/

Thanks, Elaine

like image 671
Elaine Avatar asked Feb 01 '11 08:02

Elaine


2 Answers

just replace the button handler with this line of code, and you are done.

$('#a').val('a6').scrollIntoView();
like image 197
d.popov Avatar answered Nov 13 '22 07:11

d.popov


This works fine...

$(document).ready(function() {
    $('#btn').click(function() {
        //document.getElementById('a6').scrollIntoView();
        var target = $("#b");
        $('#a> option:selected').clone(false).appendTo(target);
        target.get(0).selectedIndex = target.get(0).options.length - 1;

    });
});
like image 2
Luke Avatar answered Nov 13 '22 08:11

Luke