Yes
You could be right, actually: having the initial values of the text box and the element match could well be more user-friendly. We could use setInterval instead of setTimeout as well, and use a non-breaking space if the textbox doesn't contain anything.
Code:
<script type="text/javascript">
function startCopy(inp, elm) {
var telm;
if(typeof elm === "string")
elm = document.getElementById("elm");
for(var i = 0, e = elm.childNodes; i < e.length; ++i)
if(e[i].nodeType === 3)
break;
if(i === e.length)
telm = elm.appendChild(document.createTextNode(inp.value));
else
telm = e[i];
textCopy.copying = true;
textCopy.thread = window.setInterval(function(){textCopy(inp, telm);}, 100);
}
function textCopy(inp, elm) {
if(inp.value.length > 0)
elm.nodeValue = inp.value;
else
elm.nodeValue = String.fromCharCode(160);
if(!textCopy.copying && textCopy.thread) {
window.clearInterval(textCopy.thread);
textCopy.thread = null;
}
}
function stopCopy() {
textCopy.copying = false;
}
window.oldonload = window.onload;
window.onload = function() {
var el, tel;
if(typeof window.oldonload === "function")
window.oldonload();
(el = document.forms['modform'].elements['tlink']).value = window.prompt("Enter a default value:");
startCopy(el, tel = document.getElementById("tspan"));
stopCopy();
el.onkeydown = function() {
startCopy(this, tel);
}
};
</script>
<form name="modform" action="" onsubmit="return false;">
<p id="tspan"> </p>
<input type="text" name="tlink">
</form>
Bookmarks