Help please!! I need to ensure two divs remain the same height... javascript?

Discussion in 'JavaScript' started by typingmums, Dec 17, 2009.

  1. #1
    Hi Everyone... I'm hoping you can help me out please :)

    I've just finished a site... http://www.centralwesthire.com.au and I need code it so that the left and right content divs remain the same height as each other. I've been told that javascript is the way to go but I just cant find what I need... anyone out there have any ideas... PLEASE!! :)

    Thanks very much

    Rachelle
     
    typingmums, Dec 17, 2009 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Add this script after right_content div and try

    
    <script type="text/javascript">
        left_content_height = document.getElementById("left_content").offsetHeight+"px";
        right_content_height = document.getElementById("right_content").offsetHeight+"px";
    
        if(left_content_height>right_content_height){
            document.getElementById("left_content").style.height = left_content_height;
            document.getElementById("right_content").style.height = left_content_height;
        }
        if(left_content_height<right_content_height){      
            document.getElementById("left_content").style.height = right_content_height;
            document.getElementById("right_content").style.height = right_content_height;
        }
    </script>
    
    Code (markup):
     
    s_ruben, Dec 17, 2009 IP
    typingmums likes this.
  3. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks ever so much - I'll try it now... appreciate that :)
     
    typingmums, Dec 17, 2009 IP
  4. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #4
    You are a star!! It works a treat - thanks so much!!
     
    typingmums, Dec 18, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    this really ought to be a css solution. you ought to assign a height to the parent of the 2 content areas and then set their respective height to 100%
     
    dimitar christoff, Dec 18, 2009 IP
  6. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #6
    But that would mean setting a particular height wouldnt it? The height varies on each page.
     
    typingmums, Dec 18, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    not really, the parent element being set or auto with children div's heights at 100% would work:

    http://dci.uk.net/colheight.html

    check the css, i have assigned it a height of 600px for the parent element and both children at 100% fill it in nicely, no need for js. it gets trickier if you want to use a dynamic height for the parent element as it may cut though but typically, you'd have some constraint due to side menu or whatever anyway.
     
    dimitar christoff, Dec 18, 2009 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    Try it with long or short text!!! This example is not dynamic!!
     
    s_ruben, Dec 18, 2009 IP
  9. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #9
    no but you can make it work within the confines of a larger framework - like main content and sidebar, etc.
     
    dimitar christoff, Dec 18, 2009 IP
    typingmums likes this.
  10. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #10
    Dimitar Christoff, have you tested your example by Firefox?? Write a longer text and look at it.
     
    s_ruben, Dec 18, 2009 IP
  11. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #11
    I'll try it now Dimitar :)

    s_ruben - your example works great if there is a lot of text, on the pages where there is very little text it doesnt work at all for some reason... I'll upload an example to show you...

    http://centralwesthire.com.au/products_test.html
     
    typingmums, Dec 18, 2009 IP
  12. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #12
    You have set the script in wrong place. Set it after closing tag of <div id = "right_content">

    
    <div id = "right_content">
      <h2><a name="toilets" id="toilets"></a>Temporary  Toilets and Buildings</h2>
      <p>We  have a range of temporary, or portable, toilets, site offices and other  buildings available which are new to the Central West Hire product range. </p>
      <p>    With  the toilets being stand-alone units they have an added bonus in that they can  be placed almost anywhere on a site.</p>
      <p>&nbsp;</p>
      </div>
    
      <script type="text/javascript">
        left_content_height = document.getElementById("left_content").offsetHeight+"px";
        right_content_height = document.getElementById("right_content").offsetHeight+"px";
    
        if(left_content_height>right_content_height){
            document.getElementById("left_content").style.height = left_content_height;
            document.getElementById("right_content").style.height = left_content_height;
        }
        if(left_content_height<right_content_height){
            document.getElementById("left_content").style.height = right_content_height;
            document.getElementById("right_content").style.height = right_content_height;
        }
    </script>
    
    Code (markup):
     
    s_ruben, Dec 18, 2009 IP
  13. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #13
    Ah.. I thought you meant literally after the end of the opening div... I'll try that now thanks!! :)
     
    typingmums, Dec 18, 2009 IP
  14. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #14
    Works a treat... thanks so much!! This would have driven me nuts!

    dimitar, I tried your option but like ruben said it wasn't dynamic. Thanks very much anyway - very much appreciated :)
     
    typingmums, Dec 18, 2009 IP
  15. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #15
    Couldn't you just use a table with two columns? And use CSS (margin-right, margin-left) to get the spacing between them. I prefer not to use javascript unless it is necessary, I wouldn't bother now because it works fine... but for future reference anyway.
     
    camjohnson95, Dec 20, 2009 IP
  16. typingmums

    typingmums Well-Known Member

    Messages:
    1,446
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    140
    #16
    Yes I could use a table - but like you with javascript, I prefer to not use tables unless totally necessary. This scenario has bugged me with a few projects of mine, so I'm really pleased to have found a fix for it :)

    Do you use tables a lot when you build sites? I find it soooo much easier to use them, but I've read that it hinders SEO which is my reason for not using them.
     
    typingmums, Dec 20, 2009 IP
  17. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #17
    I use them only when I can't get div's to do what I want them to. I am not sure as far as SEO is concerned... Although many websites use tables for layouts..
    http://en.wikipedia.org/wiki/Main_Page << They use tables for content... and I guess they must be fairly seo friendly..
     
    Last edited: Dec 20, 2009
    camjohnson95, Dec 20, 2009 IP