views:

40

answers:

1

I have a div that contains some text content and I'd like to scroll and possibly highlight this within Javascript.

I found this link that describes how to scroll an entire page, but it doesn't seem to work when only using a div.

Update

I just attempted 2 approaches. Only the uncommented one worked, but it didn't really scroll enough. When rendered in IE the scrollbar hides the text.

    <script language="javascript" type="text/javascript">
        function LoadIt() {
             $("#myHeaders").scrollTop($("*:contains('pphosted'):last").offset().top);
           // $(window).scrollTop($("#myHeaders:contains('pphosted'):last").offset().top)
        }

    </script>
    <div onclick="javascript:LoadIt()">BUTTON</div>
    <div  id="myHeaders" style="width: 222px; height: 222px; overflow: scroll;">
        Microsoft Mail Internet Headers Version 2.0 Received: from E3X03.aaaa.com 
        ([10.130.15.115]) by EN35.aaaa.com with Microsoft SMTPSVC(6.0.3790.4675); Mon, 
        23 Aug 2010 18:05:20 -0400 Received: from aafff.aaaa.com ([10.111.226.11]) 
        by bbbb.aaaa.com with Microsoft SMTPSVC(6.0.3790.4675); Mon, 23 Aug 2010 
        18:05:20 -0400 Received: from mx0a-aaaaaa.pphosted.com (67.111.111.119) by 
        aaa.aaaa.com (10.130.226.11) with Microsoft SMTP Server id 8.1.436.0; 
        Mon, 23 Aug 2010 18:04:17 -0400 Received: from pps.filterd (aaa
        [127.0.0.1]) by aa.pphosted.com (8.14.3/8.14.3) with SMTP id 
        o7NM4keg010486 for
 <[email protected]>; Mon, 23 Aug 2010 18:05:19 -0400 Received: from 
        smtp113-mob.biz.mail.ne1.yahoo.com (smtp113-mob.biz.mail.ne1.yahoo.com 
        [2223]) by aaa.pphosted.com with ESMTP id qv07b08pj-1 for
 <[email protected]>; Mon, 23 Aug 2010 18:05:19 -0400 Received: (qmail 15920 invoked 
        from network); 23 Aug 2010 22:05:17 -0000 DomainKey-Signature: a=rsa-sha1; 
       </div>
    </div>
A: 

from that post, you can replace the selector '*' with your div selector. Given that the div has an id="search_div", the following should work.

$(window).scrollTop($("#search_div:contains('Are you a Lib Dem or Tory'):last").offset().top);
jdc0589
This one paritally worked... but IE's scrollbars hide the result
MakerOfThings7
Of course it does... I havent implemented this type of functionality before, but generally speaking, if IE refuses to play nice with anything you try, sometimes its better to just degrade as gracefully as possible for IE. Not saying it's appropriate here, but just something to keep in mind.
jdc0589