Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If what you want to know is whether the focus is currently in your textarea, then you can just check that by checking to see if document.activeElement is your textarea object or not.</p> <pre><code>var obj = document.getElementById("myTextArea"); if (document.activeElement === obj) { // focus is currently in my textarea } </code></pre> <p>You can see a working demo: <a href="http://jsfiddle.net/jfriend00/NCSed/" rel="nofollow">http://jsfiddle.net/jfriend00/NCSed/</a></p> <hr> <p>If what you want to know is when your textarea is being modified, here is some code that will install a change handler on your textarea and call your event handler anytime the text in the textarea is changed. This code works with either <code>textarea</code> objects or <code>input</code> objects. Unlike other methods, this captures changes done by typing, by mouse editing, cut, copy, paste and drag/drop. You can then tell when the user is typing or when any text has been typed. Here's a working test engine for this code: <a href="http://jsfiddle.net/jfriend00/xxCkm/" rel="nofollow">http://jsfiddle.net/jfriend00/xxCkm/</a>.</p> <p>And, here's the code:</p> <pre><code>// add event cross browser function addEvent(elem, event, fn) { if (elem.addEventListener) { elem.addEventListener(event, fn, false); } else { elem.attachEvent("on" + event, function() { // set the this pointer same as addEventListener when fn is called return(fn.call(elem, window.event)); }); } } function onChange(elem, fn, data) { var priorValue = elem.value; function checkNotify(e, delay) { // log('checkNotify - ' + e.type); if (elem.value != priorValue) { priorValue = elem.value; fn.call(this, e, data); } else if (delay) { // the actual data change happens aftersome events // so we queue a check for after setTimeout(function() {checkNotify(e, false)}, 1); } } // Which events to monitor // the boolean value is whether we have to // re-check after the event with a setTimeout() var events = [ "keyup", false, "blur", false, "focus", false, "drop", true, "change", false, "input", false, "paste", true, "cut", true, "copy", true ]; for (var i = 0; i &lt; events.length; i+=2) { (function(i) { addEvent(elem, events[i], function(e) { checkNotify(e, events[i+1]); }); })(i); } } </code></pre> <p>Then, sample usage is like this:</p> <pre><code>var obj = document.getElementById("test"); onChange(obj, function(e) { // your code goes here for whatever you want to do // when the change handler is called console.log("change - " + e.type); }); </code></pre> <h2>​</h2> <p>Looking at the code in your comment, I would suggest this:</p> <pre><code>&lt;script&gt; function autoRefresh(refreshPeriod) { var obj = document.getElementById("create"); function refreshIfSafe() { if (document.activeElement !== obj) { window.location.reload(); } else { setTimeout(refreshIfSafe, refreshPeriod); } } setTimeout(refreshIfSafe, refreshPeriod); } autoRefresh(2 * 1000); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload