How to highlight scrolling text

Hi..
I have the following code that scrolls some text across a textbox.
How can I highlight the current word in yellow
I can't even highlight a single char with something like this

 document.getElementById('scrolling_text_box').innerHTML = text_box += "<span class='highlight'>" + x + "</span>";

Any ideas?

Here's my code

 Scroller.prototype.scroll = function () {
        var text_scroll = this.scrollText;
        var text_box = document.getElementById('scrolling_text_box').innerHTML;
        var length_scroll = text_scroll.length;
        var length_box = text_box.length;

        this.stopped = false;

        if (length_box > this.cut_text) {
            text_box = text_box.substring(1, text_box.length);
        }

        // Letter
        var x = text_scroll.substring(this.characters, this.characters + 1);

   
        text_scroll = text_box + x;
     
          document.getElementById('scrolling_text_box').innerHTML = text_scroll;

          this.characters = this.characters + 1;

        var obj = this;
        this.timer = setTimeout(function () { obj.scroll(); }, this.speed);

        if (this.characters >= length_scroll) {
            this.stopped = true;
            clearTimeout(this.timer);
        }
    }

Open in new window

LVL 1
JElsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
I may be mistaken but I was under the impression that you could not highlight a scrolling text bar. I believe the way arounds that would be to make a scrolling CSS class and place the HTML inside that DIV
0
smeghammerCommented:
Hi JElster,

Try this - I borrowed the scrolling bit from http://www.jonathan-petitcolas.com/2013/05/06/simulate-marquee-tag-in-css-and-javascript.html and added a simple javascript somewhat like yours. I added a text box and button to test:

This CSS is from my cited reference. I altered the sizes a bit, but it is essentially not changed:
<!doctype html>
<html>
<head>
<title>highlight scroll</title>
<style>
/* scroll CSS from http://www.jonathan-petitcolas.com/2013/05/06/simulate-marquee-tag-in-css-and-javascript.html */
@keyframes marquee {
    0%   { text-indent: 400px }
    100% { text-indent: -400px }
}

@-webkit-keyframes marquee {
    0%   { text-indent: 400px }
    100% { text-indent: -400px }
}

.marquee {
    overflow: hidden;
    white-space: nowrap;
    width:400px;
    border:1px solid red;
    animation: marquee 17s linear infinite;
    -webkit-animation: marquee 17s linear infinite;
}

.marquee:hover {
    -webkit-animation-play-state: paused;
    animation-play-state: paused;
}

</style>

Open in new window

I then added a simple javascript to highlight the supplied word:
<script>
function highlight(word)
{
  try
  {
    var text = document.getElementById("marquee").innerHTML;
    if(text.indexOf(word) != -1)
    {  
      text = text.split(word).join("<span style='background-color:lightgreen;'>"+word+"</span>");
      document.getElementById("marquee").innerHTML = text;
    }
  }
  catch(e)
  {alert(e)}
}
</script>
</head>

Open in new window

Finally, a simple HTML block and form field to test:
<body>
   <div class="marquee" id="marquee">Some text that will scroll, Some text that will scroll, fish</div>
   <div>
    Type a word to highlight and press OK...
   </div>
   <input type="text" id="word_to_highlight" name="word_to_highlight" value="" />
   <input type="button" onclick="highlight(document.getElementById('word_to_highlight').value)" value="OK"/>
</body>
</html>

Open in new window



Tested on Firefox 25 - OK, Chrome 30.0 - OK and IE 9 - FAIL

If you have a more compatible scroll driven by some javascript rather than the webkit stuff here, the above highlighing function should still work.

Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.