Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Help a js noob. How do I get this function to call immediately

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #43699
    Devotee
    Member

    I have a function which checks the height of some divs and set’s them all to have the same height as the highest one.
    This is done on windows resize, which it should. But it should also do it immediately when entering the page.

    How should the code be modified?

    $( document ).ready(function() {

    function resetHeight() {
    var maxHeight = 0;
    $(“.offer-info”).height(“auto”).each(function(){
    maxHeight = $(this).height() > maxHeight ? $(this).height() : maxHeight;
    }).height(maxHeight);
    }
    resetHeight();

    // reset height on resize of the window:
    $(window).resize(function() {
    resetHeight();
    });

    });

    #129847
    CrocoDillon
    Participant

    Seems to me like you’re already calling the function right after declaring it, so that should work. On resize is commented out though, so that shouldn’t work.

    #129855
    Devotee
    Member

    It’s not commented out, I didn’t use the code formatting so it just looked like that. Fixed it now. It does not work on load so something must be wrong then. It does work whenever I resize the window though.

    #129858
    CrocoDillon
    Participant

    Maybe the height isn’t calculated yet when the script runs, try `addEventListener(‘load’, resetHeight);` or run it after a small delay. (or something like `$(window).load(resetHeight);` for xbrowser shizzle)

    EDIT: jQuery’s `.load()` is deprecated, my bad. Use `$(window).on(‘load’, resetHeight);`.

    #129870
    Devotee
    Member

    I just noticed that the height is calculated, however it’s really off. My height initially get’s a lot smaller than any of the containers are which is pretty weird. It look’s like this has something to do with Bootstrap because if I remove the bootstrap CSS then the it seems to work as it should.

    Ideas anyone?

    #129875
    CrocoDillon
    Participant

    I should have said isn’t calculated correctly. Did you at least try to run the function on load event?

    #129877
    Devotee
    Member

    Yes, it didn’t do any difference :(

    #129881
    JohnMotylJr
    Participant

    Yeah, something funky with bootstrap.. script works just fine:

    http://codepen.io/johnmotyljr/pen/gIpcB

    #129891
    TheDoc
    Member

    Might be that images aren’t being loaded until after the script runs, which will cause things to be off.

    #130005
    Devotee
    Member

    Yeah I made a testpage myself without bootstrap and it does work. It’s bootstrap.css that messes this up somehow. I’m not using any images yet so that can’t be the case either.

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.