-3

I am a beginner to JavaScript.

Can Some one help me?

How can i highlight the texts of Textarea with background, when press the star button. and Cursor should stop with highlighted background when click Stop.

And When press the stop button, it would show the words count, lines count.

for example:

The time between the two button pressed will give us the time it took for the user to read. And because the text area has the number of words, can calculate the "Reading Speed" For example: 120 words per minute, that is like 2 words a second. 1200 words in 10 minutes.

This is how I calculated that. Lets say a book page typically has: 30 lines * 8 words per line that is 240 words a page time 600 pages that is 144,000 words all together divide that by 120 words a minute reading speed that is 1200 minutes divide by 60 minutes an hour: 20 hours

The below link has related to my Question. But the text has highlighted the text into another textarea. I want to be highlight the text in same textbox.

How to highlight text in TextArea

This is my html code:

<div id="startbtn">
<button id="start" onclick="startHighlight()">Start!</button>
</div>
<div id="stoptbtn">
<button id="stop" onclick="stopHighlight()">Stop!</button>
</div>        
<div id="container">
<textarea id="inputText"></textarea>
</div>
Community
  • 1
  • 1
Pandu
  • 159
  • 1
  • 12
  • By "highlight" do you mean "select"? By "cursor" do you mean "caret"? The cursor is the mouse pointer. You haven't shown any code that you've tried, and the answer you linked to says "you can't do that". – Garr Godfrey Jun 02 '15 at 08:03
  • http://stackoverflow.com/questions/5797539/jquery-select-all-text-from-a-textarea please refer to this. – raz Jun 02 '15 at 08:04
  • Hi @raz. select the words one by one, when press start button. – Pandu Jun 02 '15 at 08:15
  • Wow... Nice question... Uses the time required to read. – Praveen Kumar Purushothaman Jun 02 '15 at 08:15
  • 1
    he basically wants a reading simulation which starts by pressing "start button" -> the triggered event should highlight word by word untill "stop button" is pressed. After that the highlighted words should be count together and the time passed should be parsed out – noa-dev Jun 02 '15 at 08:25
  • @Pandu check answers ;) – noa-dev Jun 02 '15 at 10:49

1 Answers1

4

Latest Update : Reading Simulator now reads every word based on Users inputSpeed AND based on the length of the word to provide a more realistic reading flow. https://jsfiddle.net/8Lwm6gh1/40/

Sourcode for the js:

Updated Code

  $('#sim').each(function(){
    this.contentEditable = true;
});

var go = $('#go');
var stop = $('#stop');
var wordCount = 0;
var wordCountBox = $('#wordCountBox');
var timepassed = $('#timepassed');
var textRead = $('#textRead');
var small = $('small');


go.on("click", function(e){
    e.preventDefault();
    startSim();
});

function startSim(){
    var speed = $('#speed').val();
    timeStart = $.now();
    var sim = $('#sim').text();
    var wordArray = sim.split(" ");
    var simWrap = $('#sim');
    var loopCount = 0;

    var arrCount = wordArray.length;
    var alreadyRead = [];
    loopDat();
    var i = loopCount;    
    function loopDat(){
            var pos = loopCount;
            var realSpeed = wordArray[pos].length;
            realSpeed = (realSpeed * (speed / 5));
            // realSpeed = Math.round((realSpeed * 0.1) * speed);
            console.log('Reading speed of current word: '+realSpeed);
            if(loopCount == pos){
                setTimeout(function() { 
                    if(pos < 0){
                        pos = 0;
                    }
                    alreadyRead.push(wordArray[pos]);
                    wordArray[pos] = '<b>'+wordArray[pos]+'</b>';
                    small.text('Current reading speed: '+realSpeed);
                    var words = wordArray.join(" ");
                    simWrap.html(words);                
                    wordCount++;
                    if(pos == (arrCount - 1)){
                        triggerDone();
                    }
                    loopCount++;
                    if(loopCount < arrCount){
                        loopDat();
                    }
                }, realSpeed);
            }
    }
    // Function done
    function triggerDone(){
        wordCountBox.text('Words counted: '+wordCount);
        var timeEnd = $.now();
        var timeRes = timeEnd - timeStart;
        timeRes = parseInt(timeRes);
        timeRes = timeRes / 1000;
        timepassed.text(timeRes+" seconds have passed");
        alreadyRead = alreadyRead.join(" ");
        textRead.text(alreadyRead);
        var summary = $('#summary');
        summary.show();
        return;
    } 
    stop.on("click", function(e){
        e.preventDefault();
        triggerDone();
    });
}
noa-dev
  • 3,561
  • 9
  • 34
  • 72
  • Hi @noa-dev, firsrt, thanks you for your response. its working in the JSfiddle only. not working in browser. – Pandu Jun 02 '15 at 11:59
  • it works in the browser as well - i am using jquery in that case. google jquery and implement it to your before the script – noa-dev Jun 02 '15 at 13:48
  • ya, now is working cool. thanks @noa-dev. when i am using more than 100 words, overflow the words. better if it will the textarea, but when i was used the textarea, word count shows 1 only. – Pandu Jun 02 '15 at 19:04
  • Textbox appears with scrollbar if there is too much text in it now. https://jsfiddle.net/8Lwm6gh1/38/ P.S. Next time when u are asking a question try to solve it yourself first with basic javascript tutorials etc - you didnt get any answers beside mine because all you did yourself was writting a html markup that takes 1minute ;) – noa-dev Jun 03 '15 at 06:47
  • ok. thanks for your i have added overflow:auto; in #sim class. now its fine. – Pandu Jun 03 '15 at 07:47