views:

79

answers:

3

In my application there are few <textarea> presents and applied pagination to all these <textarea>. On a page one <textarea> is displayed.

Assume there are 10 <textarea> exist, then there will be exist 10 pages, one <textarea> on each page.

I have applied auto fit feature to these <textarea>, written below function for this :

function resizeTextarea(id) {

    var minHeight = "75px"; 
    textareaObj = document.getElementById(id);

    var scrollH = textareaObj.scrollHeight+'px';
    if((textareaObj.style.height != scrollH) && (textareaObj.scrollHeight!=0)){
       textareaObj.style.height = scrollH;
    } else
    {
       textareaObj.style.height = minHeight;
    }
}

I have called this function on some events in <textarea> element defination i.e. :

<textarea class="autoGrow" onkeyup="resizeTextarea(this.id);" onchange="resizeTextarea(this.id);" onkeydown="resizeTextarea(this.id);" onmouseup="resizeTextarea(this.id);" onfocus="resizeTextarea(this.id);" style="width:78%;height:75px;overflow:hidden" onclick="resizeTextarea(this.id);" rows="6" cols ="80" id="input"><?php echo decodeChars($textdata) ;?></textarea>

Auto fit feature works properly once user triggers above mentioned events, i.e. onchange,onkeyup,onkeydown.

I stuck to one main problem that, auto fit feature wont work once page get's load.

for this I have also added below code on document.ready i.e.

$(document).ready(function() {
       if($('.autoGrow')) {
         var setOfElement = $('.autoGrow');
         for(var i = 1 ; i <= setOfElement.length ; i++) {
            resizeTextarea(setOfElement[i].id);
          }
    } 
});

Still facing same problem that, user explicitly needs to click or jumps to <textarea> to work my auto fit feature.

Please folks suggest me any pointers you have.

Your sugggestion will helps me lot.

Thanks

-Pravin

+2  A: 

You could (with most of your current code so the calculations are the same) re-write it as a plugin, like this:

jQuery.fn.resizeTextarea = function(min) {
  min = min || 75; 
  return this.bind("keyup change click focus", function() {
    var sh = this.scrollHeight;
    $(this).height(function(i, h) {
       return h <= sh && sh != 0 ? sh : min;
    });    
  }).trigger("keyup"); //run it once immediately
};

Then in your document.ready handler, just call that on the elements you want, for example:

$(function() {
  $('.autoGrow').resizeTextarea();
  //or, as it's written you can pass a different minimum height, for example:
  //$('.autoGrow').resizeTextarea(45);  
});

You can test it out here.

Nick Craver
@Nick: Thanks a lot. I ll check that, bt is this function will trigger on page onload ? I am not so confident. User dont need to trigger specific events which I mentioned above ?
pravin
@pravin - That's what the `.trigger()` portion is for (or just `.keyup()` for the short version), it'll trigger the event handler it just bound, causing a resize immediately when the plugin is run as well.
Nick Craver
i took the liberty of amending the bind method to include 'drop' in case drag/drop items were added. return this.bind("keyup change click focus drop" - http://jsfiddle.net/S37mX/
jim
A: 

I'd trigger the functions after 2/3 seconds, so that the function get triggered after the page is loaded :

setTimeout('yourFunctionThatResizesEverything()', 2)

Or you can add a script tag at the uppermost end of your page (but that's no good for w3c compliance), everything will be loaded before, and your textarea will be resized :

<script>
/*call your magical function*/
yourFunctionThatResizesEverything()
</script>
Cedric
A: 

shouldn't the for loop be

for(var i = 0 ; i < setOfElement.length ; i++) { 
            resizeTextarea(setOfElement[i].id); 
          } 

see for (var i=0 instead of for(var i=1
Arrays start at 0 right? I am not very good at JQuery, but this is my first thought...

Nivas
@Nivas : Yeah I hv modified my sample code snippet from < to <= . I think it's good now.
pravin
Its good = its working?
Nivas
yeah....working
pravin