Home > Software design >  How to make my scrollTo on a column in a table working in Javascript?
How to make my scrollTo on a column in a table working in Javascript?

Time:11-26

I'm trying to scroll a column to the bottom, but it's not working. Does anyone know how to cope with that?

Here's my simple code:

<table>
   <tbody>
       <tr>
           <td id='Trigger_td'>
               sometext
           </td>
           <td id='My_td' style='overflow:scroll;display:block;height:50vh'>
                sometext
           </td>
      </tr>
    </tbody>
</table>

And in my .js file:

  $("#Trigger_td").on("click",function(){
     //Other stuff that fill "#My_td" with content that make it overflow
     $("#My_td").get(0).scrollTo(0,$("#My_td").get(0).scrollHeight);
  });

I hope I gave you all the important info about it (first time here to write questions). I guessed that it was because when click triggers My_td is not yet "ready" to scroll. I tried to put a setTimeout, but it didn't work.

THANK YOU VERY MUCH

CodePudding user response:

Have you tried Element.scrollIntoView()?

document.getElementById('My_td').scrollIntoView();
  • Related