0

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>
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
user3192299
  • 59
  • 1
  • 2
  • 4
  • 1
    Well, _change_ is challenging for some people. – Anil Jan 16 '14 at 19:53
  • 1
    you need a `Javascript` or `jQuery` – I am Cavic Jan 16 '14 at 19:53
  • @Ljubisa I guess it could be done with html as #Sly pointed –  Jan 16 '14 at 19:56
  • @user689, one would need more than HTML to accomplish this. Cheers. – Anil Jan 16 '14 at 20:00
  • so i can't do it with javascript? – user3192299 Jan 16 '14 at 20:04
  • @Sly the guy used javascript as you see in his code, but his problemis in calling the function when he pastes the text, which could be done with html only. –  Jan 16 '14 at 20:04
  • @user689, a very simple implementation of jQuery and a change event for the text field would solve the problem. If _you_ can do what he's asking _only_ using HTML, please show me. The jQuery implementation would be cleaner and more elegant than inline onChange calls. Cheers. – Anil Jan 16 '14 at 20:10
  • @user689, I did, but I think you missed the point I was trying to get at. Please reference: http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice – Anil Jan 16 '14 at 20:15

2 Answers2

0

Your problem is that you are using the keyup function. Remember the user is pasting the text and not pressing any key. <textarea rows="5" cols="40" style="height:1em;" id="text"; name="myText" onchange="countLines(this)"> 

Use the onchange attribute, which occurs when the value of the text area is changed.

Note: as @Sly pointed doing this in javascript is more recommended:

$("textarea").change(function(){ //your function here}

Or even better

0

You need to tell it to change when something happens. If you have done this already, edit your question to say so.

I imagine your code looks something like:

<textarea onchange="your_function();">

</textarea>

Now, if you have done that and it's not working, then I would suggest running it every few seconds.

In your Javascript, if that's what you're using, then use this code:

window.setInterval(your_function, time_in_miliseconds)
ilarsona
  • 436
  • 4
  • 13
  • I think you mean `window.setInterval(your_function, time_in_miliseconds)` ;) – gen_Eric Jan 16 '14 at 20:15
  • Thanks... that would be a question that I would have to post on here 'cause I'm just that awesome and forget that you don't have to do the (). – ilarsona Jan 16 '14 at 20:17
  • It would be safe to put it directly after your function, or at the very top. However, I don't know too incredibly much about Javascript, so I would need someone to back me up on the "put it at the top" part. If it was me, I would put it right after the function. – ilarsona Jan 16 '14 at 20:26
  • I added it but the function still won't work. It only works on OnKeyUp but OnChange doesn't do anything. I put this at the end of the function: window.setInterval(countLines(this), 1) – user3192299 Jan 16 '14 at 20:34
  • You're doing it wrong. Put it in your code. Not when you call it in "onchange". – ilarsona Jan 17 '14 at 04:30