SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can anyone suggest how this script can work without a button?

    This script is perfect to auto-scroll some text being sent in via php as a means of scrolling down to the bottom so users can read the incoming data. However, this one makes use of a button. Can anyone tell me how to make the script 'automatic' as text is being submitted to the div rather then by the push of a button?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <style type="text/css">
    
    #text {
    	width: 200px;
    	height: 200px;
    	overflow: scroll;
    	padding-left: 3px;
    	border: 1px #000 solid;
    }
    
    </style>
    <script type="text/javascript">
    
    onload = function()
    {
    	document.getElementById('text').addText = function()
    	{
    		s = '<br />scrollHeight: ' + this.scrollHeight;
    		this.innerHTML += s;
    		this.scrollTop = this.scrollHeight;
    	}
    }
    
    </script>
    </head>
    <body>
    <input type="button" value="add text" onclick="document.getElementById('text').addText()" />
    <hr />
    <div id="text">
    <br /><br /><br /><br /><br /><br /><br /><br />
    <script>
    document.write('scrollHeight: ' + document.getElementById('text').scrollHeight);
    </script>
    </div>
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Maidenhead, UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this:

    Code:
    	document.getElementById('text').addText = function()
    	{
    		s = '<br />scrollHeight: ' + this.scrollHeight;
    		this.innerHTML += s;
    		this.scrollTop = this.scrollHeight;
                    setTimeout("document.getElementById('text').addText()",3000);
    	}
    
     setTimeout("document.getElementById('text').addText()",3000);
    Essentially you automate the button clicking. Change "3000" to suit the speed you need.
    -------------------------------------------------------
    UK postcode->address lookup : www.CraftyClicks.co.uk
    a piece of my brain : adam.stylo.name (at your own risk)
    -------------------------------------------------------

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2010
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been searching and playing with this script for the past few hours and finally got it work!

    Code:
    <script type="text/javascript">
    function fnSelect(objId)
    {
       fnDeSelect();
       if (document.selection) 
       {
          var range = document.body.createTextRange();
          range.moveToElementText(document.getElementById(objId));
          range.select();
       }
       else if (window.getSelection) 
       {
          var range = document.createRange();
          range.selectNode(document.getElementById(objId));
          window.getSelection().addRange(range);
       }
    }
    function fnDeSelect() 
    {
       if (document.selection)
                 document.selection.empty();
       else if (window.getSelection)
                  window.getSelection().removeAllRanges();
    } 
    </script>
    Code:
    <div id="mydiv">Hi, How are you?</div>
    <input type="button" value="Copy Code" onClick="fnSelect('mydiv')">
    But for the life of my I can't figure out how to grab all instances of the same div. For example, the function will only grab "Hi,"

    Code:
    <div id="mydiv">Hi,</div>
    <div id="div2"></div>
    <div id="mydiv">how</div>
    <div id="div2"></div>
    <div id="mydiv">are</div>
    <div id="div2"></div>
    <div id="mydiv">you?</div>
    however, what I need is a way to get all instances of the same div. Can someone please help me out with this if they have a second?

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The whole point of id is that there should only be ONE instance of an id. If you need multiple instances then use class instead. (The related call is getElementsByClassName but I'm not sure if this is universally supported).
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Thread continues here:
    http://www.sitepoint.com/forums/showthread.php?t=687760

    THREAD CLOSED
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •