views:

69

answers:

7

I have 2 divs. 1st div should automatically change own height if 2nd div height was changed.

var height = jQuery('#leftcol').height();
height += 20;   
jQuery('.rightcol-botbg').height(height);

There is no ability to change HTML markup :( I have many DHTML layout changes, so I can't run prev. code anytime. I want to run it dynamically. I need to do something like event/trigger.

I tried to use jQuery('#leftcol').resize(function(){}) or jQuery('#leftcol').change(function(){}). but it doesn't work. (resize triggers when window size changes.)

A: 

I don't understand JQuery, but seeming as this question is tagged Javascript I'll give you a Javascript solution

   document.getElementById("yourDivID").style.height = "300px";

Edit - You want to know when the height of the div has changed?

I don't know if there is a better way for that, but I would approach it with a function:

function changeBlockHeight(divID, newHeight)
{
    document.getElementById(divID).style.height = newHeight + "px";
    blockHeightChanged(divID);
}

function blockHeightChanged(divID)
{
    alert("Div " + divID + " has been changed in height!");
}

Somewhere in your code:

<div id="testBlock"></div>
<button onlcick="changeBlockHeight('testBlock')">Test</button>

Every time you change the blocks height, blockHeightChanged() is called.

Tom Gullen
Actually, there is no matter what way to use for change block height (jQuery or native javascript).But I need to know how can i add event to block when block height changes.
iexx
A: 

I would monitor the other div using an interval then update accordingly

setInterval(function(){
    var height = $('#leftcol').height();
    if(height != $('#leftcol').data('oldHeight'))
    {
        $('.rightcol-botbg').height(height+20);
        $('#leftcol').data('oldHeight',height);
    }

},500)

(untested)

The above code checks for changes in height every 0.5 seconds, then updates .rightcol-botbg;

Kristoffer S Hansen
I was thinking about it, but this way there will be additional load to browser. :(
iexx
Not very much, if your site has so much script that running a very short script every 500ms is a problem you have a completely different set of problems.
Kristoffer S Hansen
+1  A: 

When does your 1st div change ? Whatever triggers that should get the new div height, add 20 to it and set the height of the other div. Can you post some code samples ? If the size changes happen really frequently, a timer/interval that repeatedly checks one size and set's the other should also work, but this is less efficient.

sjobe
1st div changes after AJAX content loads and many other blocks hides/appers. Sure I can update 2nd block after every AJAX-call, show/hide div block, but there is lots of AJAX/blocks.Timer isn't a good idea as I think.
iexx
If you put the update code in a function, it won't be too bad adding the function call next to your ajax | show/hide calls.
sjobe
+1 I don't think your going to trigger an event caused by the div resizing, so at that point you're either:1. Checking for changes at a regular interval (suggested already, and probably bad because it puts unnecessary load on the browser)2. Checking for changes at every point in the managed code that could result in a changed height (the idea presented in this answer)I think 2 is the only way to go.
InvisibleBacon
A: 

well there is not any generic triggers for an element resize.

what is the cause of div's size change at your case? you can check your heights righta after or in that cause.

only alternative that i can think of is setting up a timer with setInterval and check both div sizes every once in a while.

var myInterval = self.setInterval("myCheckFunction()",1000);

function myCheckFunction() {
    jQuery('.rightcol-botbg').height(jQuery('#leftcol').height());
}
kali
Thanks, i didn't find a trigger too.In this case I'll update height after every AJAX call or block show/hide
iexx
A: 

Thank You all,

I didn't find a trigger too. In this case I'll update height after every AJAX call or block show/hide. Or, maybe I can set onclick event to document BODY and update height after that? It'll be more efficient than setTimeout. What do you think?

iexx
Yeah, a function call after you AJAX calls (and DOM updates) could do the trick, if you are using jQuery you can delay the update till images has finished loading aswell.
Kristoffer S Hansen
A: 

Using an interval is not very browser friendly. If you have a setup like this:

<div id="content">
   <div id="tree">should be full height</div>
   <div id="data">this one is half the height</div>
</div>

You could easily do this with css. I'm not sure if this is the case...

Otherwhise the solution proposed by Tom Gullen is the best one.

joggink
A: 

Use .bind() to create a custom event type, and then trigger that event using .trigger().

Here is a fiddle with an example (a bit contrived, but I think you will get the idea)

Wow, yes, I think it is what I need. I'll try.Thank You!
iexx
hrm, but in this case i should always trigger this event manually.http://jsfiddle.net/3u9YJ/10/Is it possible that Right column updates (height) after content changed on the left block(or left.height())?
iexx
Ah, I thought you were resizing the left one manually, and wanted the right to resize with it. It sounds like you want it to respond to a change in size due to a change in content. In that case, I think you will want to bind to the input's change event, or possibly to a keystroke event.