1

I am trying to make a typing game in javascript with jQuery but facing a issue.

How can I highlight the character the user types when they type it?

I have example in my div

 <div id="theWord">tjurkey</div>

When the user starts typing "tj.." it should highlight t, then j, as they type it. Currently I am stuck here:

 $("body").keypress(function(e) {
if (e.which !== 0) {
    var t = String.fromCharCode(e.which);
     if ( t != undefined){ wordContainer += t.replace("undefined",""); }
        if ( wordContainer == theWord){
            alert("You typed the word" + theWord);
        }
}
});

Ex. the word is "Tjurkey", if user start typing P it shouldn't highlight anything, because It's TJurkey and not P.

If user types "T" to start with it should highlight the "T" like Tjurkey, if user type "a" after that it shouldn't highlight it, because the word is Tjurkey and not Ta.... when the user then type j it should hightlight the j, because the word is TJ...urkey.. got the point?

jesper
  • 35
  • 5

3 Answers3

1

Demo: http://jsfiddle.net/cVaHb/

var $target = $('#theWord'),
    t = ''
$("body").keypress(function(e) {
if (e.which !== 0) {
    t += String.fromCharCode(e.which);
    var text = $target.text(),
        pos = text.search(t);
    if(pos > -1){
        $target.html(
            text.substring(0,pos)
            +'<span class="highlight">'+t+'</span>'
            +text.substring(pos+t.length)
        );      
    }else{
        $target.text(text);
    }
}
});

CSS:

.highlight {
    background: yellow;
}

Edit: If you want to ignore wrong letters, you can use

var $target = $('#theWord'),
    t = ''
$("body").keypress(function(e) {
if (e.which !== 0) {
    var newt = t + String.fromCharCode(e.which);
    var text = $target.text(),
        pos = text.search(newt);
    if(pos > -1){
        t = newt;
        $target.html(text.substring(0,pos)+'<span class="highlight">'+t+'</span>'+text.substring(pos+t.length));      
    }
}
});

Demo: http://jsfiddle.net/cVaHb/1/

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • It should just ignore if i type wrong, not restarting the whole thing. Can you fix it? Try typing Tju.... and then type a, then it reset its, it should just ignore it, and when i then type r (which is the next letter again) it should highlight again. – jesper Aug 13 '13 at 13:01
  • @jesper I have fixed it. But I'm not sure if you want this behavior or not: when you type (for example) `u`, `u` is highlighted, even if you haven't typed `t` and `j` – Oriol Aug 13 '13 at 14:28
1

Here, to get you started

var t = "";
var word = $("#theWord").text();
   $("body").keypress(function (e) {
       if (e.which !== 0) {
           t += String.fromCharCode(e.which);
           if (word.substring(0, t.length) == t) {
                $("#theWord").html("<span class='highlight'>" + t +"</span>"+ word.substring(t.length));               
            }
           else
           {
               t=t.substring(0,t.length - 1);               
           }
       }
   });

check it out here:

http://jsfiddle.net/zahirdhada/UBbF7/1/

zahirdhada
  • 405
  • 4
  • 14
  • It should just ignore if i type wrong, not restarting the whole thing. Can you fix it? Try typing Tju.... and then type a, then it reset its, it should just ignore it, and when i then type r (which is the next letter again) it should highlight again. – jesper Aug 13 '13 at 13:00
0

You can get the typed characters and find the starting and ending points of those in your string. Then you have to wrap that text with a span

ex: if user typed tj you should write a script to fill

<div id="theWord"><span style="color:red">tj</span>urkey</div>
Dilantha
  • 1,552
  • 2
  • 30
  • 46