Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- setTimeout to detect changes in a text field


Rain_Lover - 7:41 pm on Nov 27, 2012 (gmt 0)


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

<!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>


Demo: [jsfiddle.net ]


Thread source:: http://www.webmasterworld.com/javascript/4523008.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com