0

I have a textarea text box and a viewable p area which displays the input.
I am trying to control the the amount of text input based on p height or textarea max string length.
There are 2 issues im facing:

1) Once p goes beyond max height or textarea gets max length, textarea should stop accepting input.

2) After the max from above happens, pressing backspace or delete in textarea does not change p.

here is my fiddle

edit: this helps with part 1 but had to use keydown (textarea stops accepting input), but then hitting backspace still does not reflect on p

    if (this.value.length == max || height>50) {
            e.preventDefault();
    } else if (this.value.length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
    }else{
        recField.html(this.value);
        height = recField.height()
        $('.temp').text(height);
    }

after more tests, keydown has issues, p does not reflect input from textarea like keyup does.
: (
back to square 1

Community
  • 1
  • 1
t q
  • 4,593
  • 8
  • 56
  • 91
  • Why don't you use the [textarea's maxlength](https://developer.mozilla.org/en-US/docs/HTML/HTML_Elements/textarea)? – Dom Apr 09 '13 at 17:41
  • @Dom will look into it but then ie7 will be an issue – t q Apr 09 '13 at 17:46
  • So, until `p` does *not* go beyond max height, the *full* last line of `p` should be used for (accepting) characters/input? I'm starting to think one might then need find at what character (of input) the `p` goes beyond specified max height (recursive loop?). – GitaarLAB Apr 09 '13 at 21:53

1 Answers1

1

I've created this fiddle for you, which also works in IE7 (or at least the compatibility mode in IE10): http://jsfiddle.net/jy4qK/11/

Unfortunately it became a bit messy when I tweaked it to work in IE7, such as switching between two functions for onkeydown.

 

Edit:

A quick note about your own update regarding copying text on onkeydown:

The reason why the value of textarea is not copied to the paragraph on onkeydown, is because the keycode has not yet been sent to the element. This happens on onkeypress in some browsers (unreliable), and on onkeyup or oninput (faster than onkeyup, which is only handled every 20 ms, but is a part of HTML5 and therefore not an option if you wish to support IE < 10).

At least if my memory serves me correctly..

Mark
  • 1,093
  • 1
  • 9
  • 22
  • the Text Length seems to stop input after max, but paragraph height does not. testing on FF. also on chrome after you delete text from textarea, it does not accept input – t q Apr 09 '13 at 20:46
  • Sorry, I did not expect Firefox to be the one causing problem today :) I've updated the fiddle, I hope it helps. Also you should be able to delete input by pressing backspace once the limit is hit. – Mark Apr 09 '13 at 21:05
  • 1
    Note, in FF I can not select and change something, but *right-click > paste* (large amounts of text) is no problem with fiddle v 4. Also no problem with *select > contextmenu > paste*. I'm currently thinking along the lines of: `var doStuff=function(){\\logic here}; txtBox.change( function(){doStuff();} ); txtBox.keyup( function(){ txtBox.change(); } ); txtBox.mouseup(function(){ txtBox.change(); } ); ` – GitaarLAB Apr 09 '13 at 21:24
  • 1
    Actually it's a simple fix. I had initially taken measures against that, but I accidentally messed it up during the tweaking. `text.onblur = text.onkeydown` should be `text.onblur = text.onkeyup`. I have updated the fiddle again. – Mark Apr 09 '13 at 21:29
  • 1
    Nice! But note, In fiddle 5 I must press backspace twice for every character. Selecting text in text-area and pressing delete||backspace also didn't work. Also the locking-mechanism is to hard: If i'd delete enough text, I still can't enter any new text, *unless* I press backspace first. (Sorry) – GitaarLAB Apr 09 '13 at 21:46
  • 1
    Alrighty then.. I've fixed a few things. It should no longer require two backspace strokes to delete a character. And I've made it possible to still navigate the texarea using arrow keys, home, end, page up, and page down, when the textarea is otherwise disabled for input. Is it looking better now? Also, about the locking mechanism, could you specify how you would like it to be? I would say that it currently fulfills what the OP asked for.. though I may be wrong :) – Mark Apr 09 '13 at 22:26