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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:44:04+00:00 2026-05-26T14:44:04+00:00

I am still new to Javascript and JQuery. I have written a small function

  • 0

I am still new to Javascript and JQuery. I have written a small function I run from document-ready. Its purpose is to set the height of a div as a multiple of line-heights:

function setLoginLinksHeight(numOfLines) {

    var ll = hash(LOGIN_LINKS);
    $(ll).css('line-height','130%');
    var lh = $(ll).css('line-height');
    var nh = lh * numOfLines; // Issue here
    $(ll).height(nh);

}

From FireBug, the line-height I retrieve is 15.5833px. When I multiply it by 2 (for example), nh is set to NaN. I saw in this question that the retrieved value can have any format (%, px, etc…). This is scary!

How can I convert the returned value into a number of pixels without units into order to be able to create multiples of it? Is there a library/function available for this in Javascript or JQuery? What is the recommended practice in this case? Thanks.

EDIT

I have developed a small unit splitter following mblase75’s solution:

function splitUnit(e) {

    var eUnit = '';
    var eValue;

    if( e && ( length = e.search( /px/ ) ) ) {
        eUnit = 'px';
        eValue = e.substr( 0, length );
    } else if ( e && ( length = e.search( /em/ ) ) ) {
        eUnit = 'em';
        eValue = e.substr( 0, length );
    } else {
        eValue = e;
    }

    return new Array( eValue, eUnit );

}
  • 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-26T14:44:05+00:00Added an answer on May 26, 2026 at 2:44 pm

    Change to:

    var lh = parseFloat($(ll).css('line-height'));
    

    Of course, this doesn’t guarantee that you’ll be getting a number of pixels, just that you’ll be getting a number. To extract the units separately, add:

    var lhunits = $(ll).css('line-height').match(/\D+$/)[0]; 
    // gets first element of array
    

    …and then combine the calculation with the old units:

    var nh = (lh*numOfLines) + lhunits;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to Jquery and javascript and have jumped in head first
I'm still relatively new to javascript and jQuery and was just wondering this. Suppose
I am still new to CSS/Javascript/JQuery. I am defining a couple of colors as
(I am still very new to Javascript and JQuery) I am trying to keep
still new to XML parsing with the iphone so i have a few questions.
I am still new to LINQ, and I have wrestled with a query for
I'm fairly new to jQuery still and am trying to pick up ways to
thanks for taking the time to read this. I have a JavaScript (jQuery) navigation
I have a simple javascript function that allows me to add an item to
Fairly new to jQuery and JavaScript in general. I mocked up an example of

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.