/    Sign up×
Community /Pin to ProfileBookmark

setTimeout to detect changes in a text field

I wonder why the following sample code doesn’t work properly:

[CODE]<!DOCTYPE html>
<html>

<head>
<title></title>
<style type=”text/css”>
textarea, iframe {
display:block;
width:300px;
height:100px;
margin:3px;
padding:3px;
border:1px solid #CCC;
}
</style>
</head>

<body>
<textarea id=”field” onfocus=”getFocus();” onblur=”loseFocus();”>This is some text.</textarea>
<iframe name=”target”></iframe>
<script>
var textarea = document.getElementById(‘field’);
var iframe = window.target.document;

function displayResult() {
if (textarea.value) {
iframe.open();
iframe.write(textarea.value);
iframe.close();
}
window.setTimeout(displayResult, 10);
}

function getFocus() {
if (textarea.value == textarea.defaultValue) {
textarea.value = ”;
}
}

function loseFocus() {
if (textarea.value == ”) {
textarea.value = textarea.defaultValue;
}
}
displayResult();
</script>
</body>

</html>[/CODE]

Demo: [URL=”http://jsfiddle.net/RainLover/4ksMR/”]http://jsfiddle.net/RainLover/4ksMR/[/URL]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ReFreezedNov 27.2012 — Maybe because you're opening, writing to and closing the iframe 100 times per second (which seems more frequent than necessary)? However, the code does work for me just as it is in both Chrome and Firefox - when the textarea isn't empty it's value is copied and written to the iframe document.
Copy linkTweet thisAlerts:
@Rain_LoverauthorNov 28.2012 — Maybe because you're opening, writing to and closing the iframe 100 times per second (which seems more frequent than necessary)? However, the code does work for me just as it is in both Chrome and Firefox - when the textarea isn't empty it's value is copied and written to the iframe document.[/QUOTE]

The iframe content is supposed to get updated in real time -- as soon as the textarea content changes by keyboard or mouse. This approach is an alternative to the [I]oninput[/I] event. But since [I]oninput[/I] isn't well-supported across different browsers I decided to create a timer to compare the current text field value with its value in 10 milliseconds before.
Copy linkTweet thisAlerts:
@Vince616Nov 28.2012 — Hi bud

If you use onpropertychange for ie and oninput for everyone else you should cover most browsers

Here is an example for you:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;style type="text/css"&gt;
textarea{
width:300px;
height:100px;
border:1px solid #CCC;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function addtxt(txt){
document.getElementById("res").innerHTML=txt
}
&lt;/script&gt;
&lt;title&gt;Colour run&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;textarea id="txtarea" onpropertychange="addtxt(this.value)" oninput="addtxt(this.value)"&gt;&lt;/textarea&gt;
&lt;div id="res"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


I checked it out in ie firefox and opera and it works ok, it should work in chrome as well
Copy linkTweet thisAlerts:
@Rain_LoverauthorNov 28.2012 — Hi bud

If you use onpropertychange for ie and oninput for everyone else you should cover most browsers

Here is an example for you:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;style type="text/css"&gt;
textarea{
width:300px;
height:100px;
border:1px solid #CCC;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function addtxt(txt){
document.getElementById("res").innerHTML=txt
}
&lt;/script&gt;
&lt;title&gt;Colour run&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;textarea id="txtarea" onpropertychange="addtxt(this.value)" oninput="addtxt(this.value)"&gt;&lt;/textarea&gt;
&lt;div id="res"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


I checked it out in ie firefox and opera and it works ok, it should work in chrome as well[/QUOTE]

Thanks for the answer, but it doesn't cover IE9, which is a major browser. The oninput even is buggy in IE9: http://help.dottoro.com/ljhxklln.php
Copy linkTweet thisAlerts:
@007JulienNov 28.2012 — Add an onkeyup for Ie9 ?
×

Success!

Help @Rain_Lover spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...