0

UPDATE: since everyone is hung up on window resize

I've got a div inside a resizable div and I want to detect the resize of the child div. I know .on('resize') is only for the window. My question is solely based on the child div resize.

<div id='main_wrapper'>
  <div id = 'child_div'>
  </div>
</div>

html,
body {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}

#main_wrapper {
  height: 300px;
  width: 300px;
  background-color: blue;
}

#child_div{
  height: 100%;
  width: 100%;
}

$('#main_wrapper').resizable();

$('#child_div').on('resize', function() { //i know this isn't proper, how to do this is my question.
  alert('i changed');
})

https://jsfiddle.net/cyclingpaper/2kksqoLs/

Thanks for your time.

  • What's going to make this div change size? You're setting percentages for the height and width, these are surely only going to change on window resize? – Daniel Dewhurst Dec 10 '15 at 12:22
  • Not necessarily. If I had a jQuery UI resizealbe() function enabled on the div, then the window size wouldn't be changing. There are a couple other instances that I could list. I'm just curious on how to detect the resize of a div. (div inside a div) – Cycling Paper Dec 10 '15 at 12:27
  • @CyclingPaper Did you even see my answer man? I gave the right answer and that has now been pushed to the last because of the downvotes. Please read my answer and give me your honest comments. – Praveen Kumar Purushothaman Dec 10 '15 at 12:49
  • @PraveenKumar I've got the jsfiddle updated with the code that shouldn't work... but now is... I thought .on('resize') didn't apply to divs, but in my jsfiddle it is clearly working now. What is up with this function? Does it work for divs or not? – Cycling Paper Dec 10 '15 at 14:16

3 Answers3

1

The resize event is only targetted by the window object. You can't attach it to another DOM tree element than the most top.

https://developer.mozilla.org/en-US/docs/Web/Events/resize

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
0

Post Edited: Downvoters please remove the downvotes for no reason / anger!

The resize event is for window only, not for elements. Use this code instead:

$(window).on('resize', function(){
  alert('i changed');
});

Fiddle: https://jsfiddle.net/effsmpdm/


Take a look at this http://benalman.com/code/projects/jquery-resize/examples/resize/

It has various examples. Try resizing your window and see how elements inside container elements adjusted.

Example with js fiddle to explain how to get it work.
Take a look at this fiddle http://jsfiddle.net/sgsqJ/4/

In that resize() event is bound to an elements having class "test" and also to the window object and in resize callback of window object $('.test').resize() is called.

e.g.

$('#test_div').bind('resize', function(){
            console.log('resized');
});

$(window).resize(function(){
   $('#test_div').resize();
});

Source: How to detect DIV's dimension changed?

Community
  • 1
  • 1
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

Here you go:

https://jsfiddle.net/danhaswings/ejoxphov/2/

You can't bind the resize event to anything other than the window.

// cache objects
var $main_wrapper = $('#main_wrapper');

// on window resize
$(window).resize(function() {

    console.log('The window has resized.');

    // random example to run on window resize
    $main_wrapper.css({
        'background-color': 'red'
    });

});
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Daniel Dewhurst
  • 2,533
  • 2
  • 21
  • 39