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 9047551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:56:19+00:00 2026-06-16T11:56:19+00:00

I have a comment text box in a loop that I need to apply

  • 0

I have a comment text box in a loop that I need to apply some jQuery functions to.

  1. I need to resize the text box according to text entered.
  2. I need to check length of text. I do not want the user to enter more than 100 characters.
  3. I need the user to be able to press enter and that will submit the comment.
  4. I also need to submit the form without leaving the page.

I know how to do all functions individually. I would like to combine all the functions into one. Here is what I have:

<?php
    $i = 0; //For the id of the textarea

    while (/* Retrieve information from the database */)
    {
        $th_id = $row['th_id']; //id for the original comment/post.

        echo "<form method='post'>
        <input type='hidden' name='cb_id' value='".$th_id."' >
        <textarea onkeyup='addcom()'
                  onkeydown='addcom()'
                  placeholder='Press enter to comment'>
        </form>;
    }
?>

And here is the JavaScript code:

<script type="text/javascript">
    <!--
        function addcom()
        {
            var ch,l;
            ch = $('.addcom').val();
            l = ch.length;

            if (l == 30)
            {
                $('.addcom').css({'height':'40px'});
            }
            //More code until maximum number of characters is reached
            else if(l >= 100)
            {
                ch = ch.substring(0, 100);
                $('.addcom').val(ch);
            }
        }
    -->
</script>

How do I retrieve the information for the textfield and the hidden input field? Also, how do I trigger the event that if keycode 13 is pressed then the form gets submitted?

(The above JavaScript code is changing all the textarea in the loop as I expected.)

  • 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-16T11:56:20+00:00Added an answer on June 16, 2026 at 11:56 am

    I would create a jQuery plugin for the textboxes:

    (function($) {
        $.fn.myPlugin = function() {
            var txt = $(this);
            txt.on("keypress", function(event){
                // resize the text box according to the current value
                // if it is more than 100 characters get a substring of the first 100 characters
                // if the key press in the event is enter then submit the form using ajax
            });
        };
    })( jQuery );
    

    Then in your original loop you can just attach this plugin to your text boxes:

    var arrTextBoxes = [...];
    for (var i = 0; i < arrTextBoxes.length; i++) 
    {
        arrTextBoxes[i].myPlugin();
    }
    

    I didn’t test any code here, but the premise is sound.
    If you do this kind of stuff a lot there is another jQuery plugin called jQuery UI which has a widget factory that is perfect for creating reusable widgets (or extensible plugins) using their framework.

    Edit

    I have created a JSFiddle Demo of the plugin.

    I started off by modifying your HTML and removing the onkeyup and onkeydown events.

    Old HTML:

    <textarea onkeyup="addcom(e)" onkeydown="addcom(e)" class="com" placeholder="Press enter to submit"></textarea>
    

    New HTML:

    <textarea class="com" placeholder="Press enter to submit"></textarea>
    

    Then I created the following jquery code:

    (function($) {
        $.fn.myPlugin = function() {
            var txt = $(this);
            txt.on("keypress", function(event){
    
                // resize the text box according to the current value
                var l = txt.val().length;
                if(l < 30)
                    txt.css("height", "20px");
                else if(l == 30)
                    txt.css("height", "40px");
                else if(l == 60)
                    txt.css("height", "60px");
                else if(l == 90)
                    txt.css("height", "80px");
    
                // if it is more than 100 characters get a substring of the first 100 characters
                if(l >= 100)
                    txt.val(txt.val().substring(0, 100));
    
                // if the key press in the event is enter then submit the form using ajax
                if(event.which == 13) {
                    // Use $.get() or $.post() or $.ajax() to submit the form
                }
            });
        };
    })( jQuery );
    
    $("textarea.com").each(function(){
        $(this).myPlugin()
    });
    ​
    

    The first section will create the plugin. I recommend renaming it so that it does not use the name myPlugin. The plugin binds a keypress event to the textarea and performs your various actions.

    The second section locates all of the textarea elements and inits the plugin for each textarea.

    NOTE:
    I did not write all of the code for you to do the form submittal. If you need help with that, I would suggest creating another post on here asking specifically for help with that.

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

Sidebar

Related Questions

I have a jQuery function that takes the value from a PHP-generated check box
I have some jquery that inputs text into a database and then clears the
I have a text-box into which a user can input a comment. The comment
I have this: $text = 'This is some text /*Comment 1 */ . Some
I want to have a text box that the user can type in that
I have a multiline text box on a form. I dropped in the js
I have a comment box, if they enter long one word, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa the box
i have an gridview control with a comment text, link button, and an invisible
I have a text box where user can reply through it i should post
I have an INPUT text box. As someone types into the INPUT text box,

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.