Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect and change div height in jQuery

let me start by saying that due to the complicated design of the layout I ended up writing a function called equalHeights which basically looks at several divs in my layout and picks the highest and sets the height of rest to that, so that I can end up with divs that have equal heights. So basically after the page load, if you look into the code you will see style="height:xxx" is applied to each div. This I cannot change so any solution cannot touch this functionality.

The divs also have overflow set to hidden which again has its purpose and cannot change.

So with the above in mind, what I need to do is to be able to change the height of the divs based on changes due to user interactivity. For example I have a table with some hidden rows which get expanded as the user clicks on a link. The extra lenght of the table will then be hidden due to overflow, I need the size of the div to expand. At the same time I don't want to bind this expansion to that specific click, I want Javascript to detect change in the content and expand my div accordingly. The reason I don't want it bind like that is because I need this implemented in different pages with the same layout and I don't want to code for every event.

I have tried both:

$("#myDiv").resize(function(){
    equalHeight($("#myDiv"));
});

and

$("#myDiv").content().resize(function(){
    equalHeight($("#myDiv"));
});

neither seem to work! Any ideas?

like image 912
user839124 Avatar asked Jul 17 '11 19:07

user839124


People also ask

How to detect div height change?

The observe() method of this object is used on the element that is to be tracked. This will check for any changes and execute the callback function when any dimension change is detected.

How do I set dynamic height in jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

Which jQuery method is used to show selected elements by adjusting height?

jQuery height() Method The height() method sets or returns the height of the selected elements. When this method is used to return height, it returns the height of the FIRST matched element. When this method is used to set height, it sets the height of ALL matched elements.


2 Answers

The basic resize event can only be bound to $(window), you need to use a plugin if you want to be able to bind the resize event to a div:

http://benalman.com/projects/jquery-resize-plugin/

like image 51
Christian Nowak Avatar answered Sep 22 '22 00:09

Christian Nowak


You can trigger your own div event in function. So, it will not force you to rewrite click functions.

http://jsfiddle.net/JKByC/7/

like image 45
Nikolay Fominyh Avatar answered Sep 18 '22 00:09

Nikolay Fominyh