I have this textbox where you can paste a large block of text into it and it will output the number of lines of text. However, it only works after a single key is pressed into the box. So if I right click and hit "paste" then it won't display the number of lines until I press a key on my keyboard.
How can I just make it automatically display the number of lines upon pasting the text?
Here's my current code:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
textarea {
border: 0 none white;
overflow: hidden;
padding: 0;
outline: none;
background-color: #D0D0D0;
resize: none;
}
</style>
<script>
function countLines(theArea)
{
var theLines = theArea.value.replace((new RegExp(".{"+theArea.cols+"}","g")),"\n").split("\n");
if(theLines[theLines.length-1]=="")
theLines.length--;
theArea.form.lineCount.value = theLines.length;
}
</script>
<script type="text/javascript">
var observe;
if (window.attachEvent)
{
observe = function (element, event, handler)
{
element.attachEvent('on'+event, handler);
};
}
else
{
observe = function (element, event, handler)
{
element.addEventListener(event, handler, false);
};
}
function init ()
{
var text = document.getElementById('text');
function resize ()
{
text.style.height = 'auto';
text.style.height = text.scrollHeight+'px';
}
/* 0-timeout to get the already changed text */
function delayedResize ()
{
window.setTimeout(resize, 0);
}
observe(text, 'change', resize);
observe(text, 'cut', delayedResize);
observe(text, 'paste', delayedResize);
observe(text, 'drop', delayedResize);
observe(text, 'keydown', delayedResize);
text.focus();
text.select();
resize();
}
</script>
</head>
<body onload="init();">
<form>
<textarea rows="5" cols="40" style="height:1em;" id="text"; name="myText" onKeyUp="countLines(this)">
</textarea>
<br>
Cost: <input type=text name="lineCount" size="1" value="0"> Dollars
</form>
</body>
</html>