Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:15:37+00:00 2026-06-14T11:15:37+00:00

Background: I’m making a facebook wall-alike page, which will have many posts and you

  • 0

Background:
I’m making a facebook wall-alike page, which will have many posts and you should be able to comment every post. And these textboxes, where you post type your comment in, should resize as in Facebook.

I have this code so far. It works but only in the FIRST textbox on the page, I would like it to work on every of them 🙂

Any chances to get it working for every textbox?

<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();">
<textarea rows="1" style="height:1em;" id="text"></textarea>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:15:40+00:00Added an answer on June 14, 2026 at 11:15 am

    Can you provide sample code of a live page? As it stands, the javascript you’ve shown here only works on the single element identified with the ID of text; therefore, no matter how many textareas you have, the only element affected will one of the elements on the page with that ID — of which you should have only one, of course.

    Approaching the code as it stands now, you need to change from document.getElementById to something like document.getElementsByTagName('textarea') then loop over the result. Try the following:

    function init () {
    var textareas = document.getElementsByTagName('textarea');
    ...
    var textareasLength = textareas.length;
    for (var i = 0; i < textareasLength; i++) {
        observe(textareas[i], 'change',  resize);
        observe(textareas[i], 'cut',     delayedResize);
        observe(textareas[i], 'paste',   delayedResize);
        observe(textareas[i], 'drop',    delayedResize);
        observe(textareas[i], 'keydown', delayedResize);
    }    
    }
    

    EDIT:

    On review, among other things, your resize() function was only working on text variable as defined in the line immediately prior. Without totally refactoring your code, one way to get the functionality you want is the following:

    function init () {
    var textareas = document.getElementsByTagName('textarea');
    function resize() {
        if( arguments.length>0 ) {
          self=arguments[0];
        } else self=this;      
        self.style.height = 'auto';
        self.style.height = self.scrollHeight+'px';
    }
    /* 0-timeout to get the already changed text */
    function delayedResize() {
        var self=this;
        window.setTimeout(resize(self), 0);
    }
    var textareasLength = textareas.length;
    for (var i = 0; i < textareasLength; i++) {
    observe(textareas[i], 'change',  resize);
    observe(textareas[i], 'cut',     delayedResize);
    observe(textareas[i], 'paste',   delayedResize);
    observe(textareas[i], 'drop',    delayedResize);
    observe(textareas[i], 'keydown', delayedResize);
    }    
    }
    

    This isn’t optimal, but it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a ror application which is continuously recording and showing on a
BACKGROUND : My application allows users to select which directory in a multi-page tiff
Background I have a database for my company site in which I store parts
Background I have a form that PHP generates from my database. The page is
Background I have the following components: My local solution (.NET 4.5) which makes use
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background: I would like to dismiss a modalView that I have presented earlier and
Background: I have a Visual Studio solution consisting of a website + REST web-service
Background: I have an application with an alternate entry point. It listenes for SMS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.