开发者

<textarea> what key was pressed with javascript

I would like to ask somebody how i can determine what key was pressed in a textarea.... need to write a little javascript code.. a user ty开发者_JS百科pe in a textarea and i need to write it in a while he writing so the keydown, keypress event handle this functionality, also need to change the text color if a user typed a "watched" word (or the word what he wrote contains the "watched" word/words ) in the textarea.. any idea how i can handle it ??

till now did the text is appear in the <div>, but with this i have a problem.. can't check if the text is in the "watched"... the document.getElementById('IDOFTHETEXTAREATAG'); on keypress is not really works because i got back the whole text inside of the textarea.....

So how i can do it ? any ideas ??? "(Pref. in Mozilla FireFox)


Well, if you were using jQuery, you could do this given that the id of your textarea was 'ta':

$('#ta').keypress(function (evt) {
  var $myTextArea = $(this); // encapsulates the textarea in the jQuery object
  var fullText = $myTextArea.val(); // here is the full text of the textarea
  if (/* do your matching on the full text here */) {
    $myTextArea.css('color', 'red'); // changes the textarea font color to red
  }
};


I suggest you use the 'onkeyup' event.

$( element ).keyup( function( evt ) {
    var keyPressed = evt.keyCode;
    //...
});


I have this made like this (plain JS, no JQuery):

function keyDown(e) { 
    var evt=(e)?e:(window.event)?window.event:null; 
    if(evt){ 
       if (window.event.srcElement.tagName != 'TEXTAREA') {
        var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
        }
     }
} 
document.onkeydown=keyDown;

This script is in head tag. I am catching this in all textarea tags. Modify it for your purpose.


2 textareas.

In the first textarea I need to write the words or chars what you want to "watch" in the typing text.

In the second textarea I need to type text, so when I type text, under the textarea need to write what is in the textarea (real time) and highlight the whole word if contains the watched words or chars.

For example:

watched: text locker p

text: lockerroom (need to highlite the whole word because it contains the locker word) or apple (contains the p)

who I can do if a word not start with watched word/char to highlite the whole word?

JavaScript:

var text;
var value;
var myArray;
var found = new Boolean(false);

function getWatchedWords()
{
    myArray = new Array();
    text = document.getElementById('watched');
    value = text.value;
    myArray = value.split(" ");
    for (var i = 0;i < myArray.length; i++) 
        {
  document.getElementById('writewatched').innerHTML += myArray[i] + "<newline>";
        }
}


function checkTypeing()
{
    var text2 = document.getElementById('typeing');
    var value2 = text2.value;
    var last = new Array();
    last = value2.split(" ");
    if (last[last.length-1] == "")
    {
       if(found)
       {
         document.getElementById('writetyped').innerHTML += "</span>";
         document.getElementById('writetyped').innerHTML += " ";
       }
       else
         document.getElementById('writetyped').innerHTML += " ";  


    }
    else
       check(last[last.length-1]);
}

function check(string)
{
  for (var i = 0; i < myArray.length; i++) 
  {
     var occur = string.match(myArray[i]);
     if(occur != null && occur.length > 0)
     {
        if (!found)
        {
            found = true;
            document.getElementById('writetyped').innerHTML += "<span style='color: blue;'>";
        }
        else
        {
           found = true;
        }
     }
     else
     {

     }
  }

  if(found)
  {
    document.getElementById('writetyped').innerHTML += string;
  }
  else
  {
    document.getElementById('writetyped').innerHTML += string;
  }     
}

HTML:

<html>
<head>
<title>TextEditor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src='script.js' type='text/javascript'></script>
</head>
<body>
<div>
<p>Watched words:</p>
<textarea id="watched" onblur=getWatchedWords();>
</textarea>
</div>
<div id="writewatched">
</div>
<div>
<p>Text:</p>
<textarea id="typeing" onkeyup=checkTypeing();>
</textarea>
</div>
<div id="writetyped">
</div>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜