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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:59+00:00 2026-05-25T10:35:59+00:00

Good Day. I’m using table-less forms and want to size the <label> block-element. This

  • 0

Good Day.
I’m using table-less forms and want to size the <label> block-element. This I can do, like:
HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for='email'>Email:</label>
        <input type="text" id="email" blah...>
      </div>
      <div class="ffContainer">
        <label for='name'>Name:</label>
        <input type="text" id="name" blah...>
      </div>
    </div><!-- end fContainer -->
   </form>
</div><!-- end main-content -->

jQuery:

$(document).ready(function(){
  $(".ffContainer label").each(function(){
     if( $(this).width() > w) {
       w = $(this).width();
     };
  });
  // have widest label now make all of them the same size so that they fit
  $(".ffContainer label").attr("style","width:" + Math.round( $(window).width() / w) + "%;");

}); // end document.ready

This does the job just fine. All the <label> fields are the same width and my table-less form looks pretty.

However, if I want to add a second form on the page, I try to loop thru each set of forms, grab the widest width, and set the <label> to that width; then move on to the next form on the page and do the same.

Problem: I can’t seem to get the second form’s <label> width to be anything different from the first form’s <label> width.

I tried this HTML:

<div id="main-content">
  <form id="profile" blah...>
    <div id="fContainer">
       <div class="ffContainer">
         <label for='email'>Email:</label>
         <input type="text" id="email" blah...>
       </div>
       <div class="ffContainer">
         <label for='name'>Name:</label>
         <input type="text" id="name" blah...>
       </div>
    </div><!-- end fContainer -->
  </form>

  <form id="delta" blah...>
    <div id="fContainer">
      <div class="ffContainer">
        <label for="car">Car:</label>
        <input type="text" id="car" blah...>
      </div>
      <div class="ffContainer">
        <label for='engine'>Engine:</label>
        <input type="text" id="engine" blah...>
      </div>
    </div>
   </form>
 </div><!-- end main-content -->

And here’s the jQuery that isn’t doing the job:

$(document).ready(function(){
  $("#main-content #fContainer").each(function(){  // loop thru both #fContainer blocks
    $(".ffContainer label").each(function(){       // loop thru all label elements
       if( $(this).width() > w){
         w = $(this).width();
       };                                          // have widest label as var w
       $(".ffContainer label").each(function(){    // loop thru all labels again to indivdually set each attr
          $(this).attr("style","width:" + Math.round($(window).width() / w) + "%;");
       });
     }); // end .ffContainer label loop MOVE ON TO NEXT FORM GROUP
  }); // end #fContainer loop
 }); // end document.ready

Any help would be great!
There might be a typo above, but I’m certain there’s not in my code.
I really don’t want to name the second form divs something else and repeat all the jQuery code, but don’t have a problem naming the second divs something else if I don’t have to repeat all the jQuery code (hope I’m making sense there).

Thanks.

  • 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-25T10:35:59+00:00Added an answer on May 25, 2026 at 10:35 am

    Fix your HTML (remove the duplicate IDs), then try

    $('form').each(........)
    

    instead of

    $("#main-content #fContainer").each(........)
    

    Change

    $(".ffContainer label").each(...)
    

    into

    $(".ffContainer label", this).each(...)
    

    so the jQuery selector is within the scope of one form at a time.

    Also, it might be a good idea to have var w = 0; before $(".ffContainer label", this).each(...) to ensure that the value of w doesn’t leak from one run to the next.

    UPDATE: I went ahead and plugged your code into a test page. There was a nesting issue that was hard to see from the question. I cleaned it up a bit and the code below appears to be working.

    $(document).ready(function(){
      $("form").each(function(){  // loop thru both #fContainer blocks
        var w = 0;
        $(".ffContainer label", this).each(function(){       // loop thru all label elements
          if( $(this).width() > w){
            w = $(this).width();
          }
        });
    
        var percent_width = (Math.round(w / $(window).width() * 100)) + "%"; // This calc only needs to be performed once per form.
        $(".ffContainer label", this).each(function(){    // loop thru all labels again to indivdually set each attr
          $(this).width(percent_width);
        });
      }); // end #fContainer loop
    }); // end document.ready
    

    UPDATE: For details about jQuery selector context
    Check out this blog post referenced in this other question

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

Sidebar

Related Questions

Good day, I need to extract portion of string which can looks like this:
Good day, I want to set click event on every anchor element in my
Good day, I have a hibernate mapping which goes something like this <class name=Person>
Good day. I'd like to ask a question. Why TextBox control Txt in this
Good day, PowerShell rookie here... If I output to the screen like this: foreach($databasePermission
Good day. I just want to consult about using websphere as san application server
Good day! I want to add and remove CSS files according to the size
Good day, I'm having a problem with asp.net 2.0 viewstate. Basically, I want to
Good day everyone, I am building a page in ASP.NET, and using Master Pages
Good day, I am using CacheFilter to filter a certain path to my server

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.