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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:22:11+00:00 2026-05-26T07:22:11+00:00

I am using jQuery 1.6.2 When my page loads, I run a loop that

  • 0

I am using jQuery 1.6.2

When my page loads, I run a loop that finds input fields, calculates their needed size, and resizes them. Here’s the function:

// MINIMIZE TEXT FIELDS ON LOAD
$(":text").each(function() {
    var MyString = $(this).val();
    var MyMax = $(this).attr("maxlength");
    var MySize = minimizeText(MyString, MyMax);
    $(this).attr("size", MySize);
});

The loop accesses this function:

var minimizeText = function minimizeText(MyString, MyMax) {
var MyLength = MyString.length;
var MySize = MyLength + 5;
if (MySize < MyMax && MySize < 50) {
    return MySize;
} else if (MyMax > 50 ) {
    return 50;
} else {
    return MyMax;
}
}

If the user changes the text in the input field, the size is recalculated to fit the text in the field.

// MINIMIZE TEXT FIELDS ON KEYUP
$(":text").keyup(function() {
    var MyString = $(this).val();
    var MyMax = $(this).attr("maxlength");
    var MySize = minimizeText(MyString, MyMax);
    $(this).attr("size", MySize);
});

Both of these work perfectly well. My goal here is to condense this code and still make it readable. My question is how can I combine these two so that the text field are checked on load as well as on keyup?

  • 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-05-26T07:22:12+00:00Added an answer on May 26, 2026 at 7:22 am

    Your keyup handler is the same as the function that you pass into the initial call to .each(). You can define a single function and pass it in as an argument. eg.

    function resize_handler() { ... }
    $(':text').keyup(resize_handler).each(resize_handler);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to animate a block level element using jQuery. The page loads with
There's placeholder on the page that is loaded asynchronously using jQuery load method. Page
I am using jQuery UI to build tabs on my page. That works well
I have a page which loads children pages by using jQuery's .load function... My
I am using jQuery to disable validation controls in an aspx page on load.
I am using jQuery to load a page by AJAX using $.ajax (suppose test.html).
I am using jQuery to show/hide a div container (#pluginOptionsContainer), and load a page
I am trying to load mschart in my aspx page(SalesOverview.aspx) using jQuery.load() method, I
I am using jQuery to invoke page methods for my AJAX calls using $.Ajax.
I'm working on a page using jQuery's accordion UI element . I modeled my

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.