Akira Dawson
Akira Dawson

Reputation: 1267

Can't scroll to dynamic element

Hey guys I'm having some issues scrolling to a an element that's dynamically created. You click on a search button, AJAX does its thing and updates the content. I have some code to dynamically find the ID and scroll to it. I am able to get the ID but I can't scroll to it. So far I have the code:

to_Scroll = $(this).closest('tr').attr('id');
$('html, body').animate({
    scrollTop: $(to_Scroll).offset().top
}, 2000);

Which seems to work when I put it in the console with hard coded data. But doing it dynamically yields no results and no errors. Any help would be greatly appreciated

Below is some code which is done before I animate and scroll to an element:

dateChange(blah, blah2, blah3);
to_Scroll = $(this).closest('tr').attr('id');
    $('html, body').animate({
        scrollTop: $(to_Scroll).offset().top
    }, 2000);

function dateChange(dateInput, nGuests, vName){
    var promises = [];
    var promise = $.ajax({
          url: "/blah.asp?blah="+blah+"&blah2="+blah2+"&blah3="+blah3,
          dataType:"html",
          success: function(data){
              table.html(data);
          }
        });
    promises.push(promise);

$.when.apply($, promises).done(function() {
    $( "#boatContent" ).removeClass( "loading" ); //Everything is done, remove loading gif
    //do other stuff
}

Upvotes: 0

Views: 439

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

to use ID again from attr you need to add # to it

$(document).ready(function(){
    var to_Scroll = $(this).closest('tr').attr('id');
    $('html, body').animate({
        scrollTop: $( '#' + to_Scroll).offset().top
    }, 2000);
});

Upvotes: 3

Related Questions