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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:34:56+00:00 2026-05-30T19:34:56+00:00

I have a few elements. The number of them on the page changes constantly,

  • 0

I have a few elements. The number of them on the page changes constantly, keep that in mind.

The element is called .portalimg. I need to say if the width > height, set it’s width to 125px. If the height > width, set the height to 125px.

Here’s what I have:

$('.portalimg').each(function() {
  var pimgw = $(this).width();
    var pimgh = $(this).height();
    if (pimgw > pimgh) {
        $(this).css('width','125px')
    } else {
        $(this).css('height','125px')
    }
});

EDIT: It alerts success but doesn’t apply the width & height.
Where did I go wrong?

EDIT 2: Updated code with cleanest from below. That still only constrains the height property, even if the images is taller then it is wide. Take a look:

http://jacksongariety.com/gundoglabs/

EDIT 3: The issue is they all return 0. If I have an alert say the value of pimgw or pimgh, it’s always 0.

EDIT 4: Finally got the best, cleanest code possible with caching and it’ll always load correctly, even if it draws images form the cache:

$(function(){
    $('.portalimg').ready(function(){
        $('.portalimg').fadeTo(0,1).each(function(index) {
            var a = $(this);
            if (img.width() > a.height()) {
                a.css('width','135px')
            } else {
                a.css('height','125px')
            }
        });
    });
});
  • 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-30T19:34:57+00:00Added an answer on May 30, 2026 at 7:34 pm

    If there are multiple elements of class portalimg you could simply use a .each iterator: http://api.jquery.com/each/

    $(function() {
        $('.portalimg').each(function(index) {
            var img = $(this);
            if (img.width() > img.height()) {
                img.css('width','125px')
            } else {
                img.css('height','125px')
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Page is 980px in Width, but I have a few design elements (which
I have a few <input> elements that look like this: <input id=hosts name=250 class=hostselector
I have a HTML page that displays a few values. I also have a
I have a few list elements, like this: <li class=item> <a class=toggle><h4>ämne<small>2010-04-17 kl 12:54
I have made a list with few elements in it. Now everything looks right,
I have an unordered list with a few list elements. #tags { width: 300px;
I have to implement a few action. Each of they have some common elements,
I have a select element with a few options. Depending on what is selected,
I have few question in this regard When you create an internet page, does
I have few nested DIVs at page. I want to add event only for

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.