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

  • Home
  • SEARCH
  • 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 6027559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:36:38+00:00 2026-05-23T04:36:38+00:00

so I want to use this JQuery plugin that Stack Overflow has made available

  • 0

so I want to use this JQuery plugin that Stack Overflow has made available to me 🙂

Auto-size dynamic text to fill fixed size container

However, I need to use this on an HTML element that is dynamically created server side. Example,

I create all the elements, convert them to HTML, and then return them as an array of Json strings basically (in HTML form) to the front end:

public JsonResult GetMessages()
{
    // do conversion work
    List<string> htmlMessages = new List<string>();
    foreach(Message message in this.messages)
    {
        htmlMessages.Add(message.toHTML());
    }
    return Json(htmlMessages);
}

And then on the front end I append them to a div in Jquery like this:

// make my AJAX call
// in the success method I do this
for (var i = 0; i < data.length; i++)
{
    $('#containerDiv').append(data[i]);
}

Now this works fine but how would I then call the JQuery plugin on each of these elements that’s appended to format the text size within one of the divs in the element?

  • 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-23T04:36:39+00:00Added an answer on May 23, 2026 at 4:36 am

    I don’t know what the HTML itself looks like, but try something like this:

    // snip...
    var $container = $('#containerDiv');
    for (var i = 0; i < data.length; i++)
    {
        $container.append(data[i]);
    }
    $container.children('div').textfill({ maxFontPixels: 36 });
    

    It looks like the plugin relies on the element containing a single <span> element, so your HTML will probably have to be structured accordingly. Also based on a wee little test, it looks like the plugin doesn’t work correctly when called on a jQuery collection that contains more than one element. The simple fix:

    $container.children('div').each(function ()
    {
        $(this).textfill({ maxFontPixels: 36 });
    });
    

    The “Right Way” is to actually change the plugin, though:

    ;(function($) {
        $.fn.textfill = function(options) {
            var fontSize = options.maxFontPixels;
            this.each(function () {
                var $this = $(this),
                    ourText = $this.find('span:visible:first'),
                    maxHeight = $this.height(),
                    maxWidth = $this.width(),
                    textHeight,
                    textWidth;
                do {
                    ourText.css('font-size', fontSize);
                    textHeight = ourText.height();
                    textWidth = ourText.width();
                    fontSize = fontSize - 1;
                } while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3);
            });
            return this;
        }
    })(jQuery);
    

    So your changed plugin, will that call it on every single span it finds in the container div?

    Not quite. My changes just mean that you’d be able to replace

    $container.children('div').each(function ()
    {
        $(this).textfill({ maxFontPixels: 36 });
    });
    

    with

    $container.children('div').textfill({ maxFontPixels: 36 });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already started this in javascript so I don't want to use jquery but
I found this open-source library that I want to use in my Java application.
I am working with a jquery plugin that looks like this: (function($){ var options;
i want to use this plugin on my asp.net application. I am serializing my
Good afternoon people's I have built a jquery plugin that I use on my
I want to use a temp directory that will be unique to this build.
Does anyone know of a up-to-date jquery plugin that does exactly what this does:
I made my first jQuery plugin attempt, but I have this problem/bug which I
I'm using the jQuery Cupertino theme and want to use the icons from that
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I

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.