AutoScroll with checkbox.

Discussion in 'JavaScript' started by tanyania, Jul 16, 2011.

  1. #1
    Hi guys,

    does anyone know how to have a div that auto scrolls to the bottom every 1 second or so, BUT only when a checkbox is checked?

    Lets say i have a div named. text and a checkbox named scrollcheck.

    I would like the text div to auto scroll to the bottom only when scrollcheck is checked?

    Any thoughts or ideas? ( My JS is rubbish btw )


    Thanks
     
    tanyania, Jul 16, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    You can use following codes:

    
    <div><input id="check" type="checkbox" onclick="update()">Auto-scroll</div>
    <div id="text" style="width: 200px; height: 100px; border: solid 1px grey; overflow: scroll;">
    German Chancellor Angela Merkel said Friday that it is Berlin's duty to support the euro currency as she praised the new eurozone agreement on a second bailout for Greece.
    
    Merkel said the deal reached Thursday in Brussels to help Greece was a "significant" step that would help Europe and the currency used by the 17-nation eurozone.
    </div>
    <script>
    function update() {
      var box = document.getElementById('check');
      if (!box.checked) return;
      var tag = document.getElementById('text');
      tag.scrollTop = tag.scrollHeight;
      setTimeout('update()', 1000);
    }
    update();
    </script>
    
    Code (markup):
     
    Last edited: Jul 22, 2011
    dthoai, Jul 22, 2011 IP