1

I try to scroll a list made by dynamically. I've found this answer, but it did not help me. I guess that this is because my list is a dynamic list. How can I do? I want to scroll dynamic list on select value.

This is a fiddle with my code: http://goo.gl/Tv7wvj and this is my code

HTML

<div class="scroll order">
    <ul></ul>
</div>

CSS

.scroll{
    overflow-y: scroll;
    width:150px;
    height:80px;
}

Javascript

$(document).ready( function() {
    var html = '';
    for(i=0; i<10; i++){
        if(i==6){
            html += '<li> <label><input type="radio" name="radio" value="li'+i+'" checked="checked" /> 0'+i+' </label> </li>';
            continue;
        }
        html += '<li> <label><input type="radio" name="radio" value="li'+i+'" /> 0'+i+' </label> </li>';
    }
    $('ul').append(html);
});

var $s = $('.scroll');
var optionTop = $s.find('[value="04"]').offset().top;
var selectTop = $s.offset().top;

alert("optionTop : "+optionTop+" / selectTop :  "+selectTop);

$s.scrollTop($s.scrollTop() + (optionTop - selectTop));
Community
  • 1
  • 1
Soyeon Kim
  • 608
  • 7
  • 34
  • 1
    how exactly is it going to find value="aaaa" ????? that doesn't exist on you list --- see this that works https://jsfiddle.net/nn5fpmqa/ – Tasos Feb 23 '15 at 01:08

1 Answers1

0

(Question answered in the comments. Converted to a community wiki answer.)

@Tasos wrote:

how exactly is it going to find value="aaaa" ????? that doesn't exist on you list --- see this that works jsfiddle.net/nn5fpmqa:

$(document).ready( function() {
    var html = '';
    for(i=0; i<10; i++){
        if(i==6){
            html += '<li> <label><input type="radio" name="radio" value="li'+i+'" checked="checked" /> 0'+i+' </label> </li>';
            continue;
        }
        html += '<li> <label><input type="radio" name="radio" value="li'+i+'" /> 0'+i+' </label> </li>';
    }
    $('ul').append(html);
});
setTimeout(function() {
var $s = $('.scroll');
var optionTop = $s.find('[value="li6"]').offset().top;
var selectTop = $s.offset().top;

alert("optionTop : "+optionTop+" / selectTop :  "+selectTop);

$s.scrollTop($s.scrollTop() + (optionTop - selectTop));
    }, 2000);
Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129